<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Python学习个人简记 | Forgo7ten'blog</title><meta name="keywords" content="Python"><meta name="author" content="Forgo7ten"><meta name="copyright" content="Forgo7ten"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Python学习 ¶  类型 ¶  类型转换 ¶  int()：将…转换为整数 float()：将…转换为浮点数 str()：将…转换为字符串 判断类型 ¶  查看变量类型type()，传入变量会告诉类型 判断变量类型isinstance(elem, type)：判断elem是否为type类型，返回bool类型的真假 字符串 ¶  原始字符串：字符串前加一个r，可以不用转义    方法&#x2F;函数 参数">
<meta property="og:type" content="article">
<meta property="og:title" content="Python学习个人简记">
<meta property="og:url" content="https://forgo7ten.github.io/Development/python/Python_Development_Notes/index.html">
<meta property="og:site_name" content="Forgo7ten&#39;blog">
<meta property="og:description" content="Python学习 ¶  类型 ¶  类型转换 ¶  int()：将…转换为整数 float()：将…转换为浮点数 str()：将…转换为字符串 判断类型 ¶  查看变量类型type()，传入变量会告诉类型 判断变量类型isinstance(elem, type)：判断elem是否为type类型，返回bool类型的真假 字符串 ¶  原始字符串：字符串前加一个r，可以不用转义    方法&#x2F;函数 参数">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://forgo7ten.gitee.io/store/imgs/12.jpg">
<meta property="article:published_time" content="2021-01-07T16:00:00.000Z">
<meta property="article:modified_time" content="2021-01-07T16:00:00.000Z">
<meta property="article:author" content="Forgo7ten">
<meta property="article:tag" content="Python">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://forgo7ten.gitee.io/store/imgs/12.jpg"><link rel="shortcut icon" href="/images/favicon.jpg"><link rel="canonical" href="https://forgo7ten.github.io/Development/python/Python_Development_Notes/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//hm.baidu.com"/><link rel="preconnect" href="//www.clarity.ms"/><link rel="preconnect" href="//fonts.googleapis.com" crossorigin=""/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta name="google-site-verification" content="8L0ZjthONyq6-m7FniRTH8A7Bh0x-KOPZbKOcNimnYA"/><meta name="baidu-site-verification" content="code-gjVsyPt7yr"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?b076845d73a4f28b3c7401b2a0a8a9db";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script>(function(c,l,a,r,i,t,y){
    c[a]=c[a]||function(){(c[a].q=c[a].q||[]).push(arguments)};
    t=l.createElement(r);t.async=1;t.src="https://www.clarity.ms/tag/"+i;
    y=l.getElementsByTagName(r)[0];y.parentNode.insertBefore(t,y);
})(window, document, "clarity", "script", "9zbibmmffi");</script><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Titillium+Web&amp;display=swap" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容：${query}"}},
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":300},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: true,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: true,
  isAnchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Python学习个人简记',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-01-08 00:00:00'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="Forgo7ten'blog" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/images/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">20</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">8</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">6</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-compass"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/atom.xml"><i class="fa-fw fa fa-rss"></i><span> RSS订阅</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fas fa-comment-dots"></i><span> 留言板</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('http://forgo7ten.gitee.io/store/imgs/12.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Forgo7ten'blog</a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-compass"></i><span> 目录</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></li><li><a class="site-page child" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></li><li><a class="site-page child" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/atom.xml"><i class="fa-fw fa fa-rss"></i><span> RSS订阅</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div><div class="menus_item"><a class="site-page" href="/messageboard/"><i class="fa-fw fas fa-comment-dots"></i><span> 留言板</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Python学习个人简记</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-01-07T16:00:00.000Z" title="发表于 2021-01-08 00:00:00">2021-01-08</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-01-07T16:00:00.000Z" title="更新于 2021-01-08 00:00:00">2021-01-08</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%BC%80%E5%8F%91/">开发</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">16.3k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>58分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Python学习个人简记"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Python学习">Python学习<a class="header-anchor" href="#Python学习"> ¶ </a></h1>
<h2 id="类型">类型<a class="header-anchor" href="#类型"> ¶ </a></h2>
<h3 id="类型转换">类型转换<a class="header-anchor" href="#类型转换"> ¶ </a></h3>
<p><code>int()</code>：将…转换为整数</p>
<p><code>float()</code>：将…转换为浮点数</p>
<p><code>str()</code>：将…转换为字符串</p>
<h3 id="判断类型">判断类型<a class="header-anchor" href="#判断类型"> ¶ </a></h3>
<p>查看变量类型<code>type()</code>，传入变量会告诉类型</p>
<p>判断变量类型<code>isinstance(elem, type)</code>：判断elem是否为type类型，返回bool类型的真假</p>
<h2 id="字符串">字符串<a class="header-anchor" href="#字符串"> ¶ </a></h2>
<p>原始字符串：字符串前加一个<code>r</code>，可以不用转义</p>
<table>
<thead>
<tr>
<th style="text-align:center">方法/函数</th>
<th style="text-align:center">参数</th>
<th style="text-align:center">返回值</th>
<th style="text-align:center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">.title()</td>
<td style="text-align:center"></td>
<td style="text-align:center">新字符串</td>
<td style="text-align:center">将字符串各单词首字母改为大写</td>
</tr>
<tr>
<td style="text-align:center">.upper()</td>
<td style="text-align:center"></td>
<td style="text-align:center">新字符串</td>
<td style="text-align:center">将字符串中字母全部改为大写</td>
</tr>
<tr>
<td style="text-align:center">.lower()</td>
<td style="text-align:center"></td>
<td style="text-align:center">新字符串</td>
<td style="text-align:center">将字符串中字母全部改为小写</td>
</tr>
<tr>
<td style="text-align:center">.rstrip()</td>
<td style="text-align:center"></td>
<td style="text-align:center">新字符串</td>
<td style="text-align:center">将字符串右侧空白删除</td>
</tr>
<tr>
<td style="text-align:center">.lstrip()</td>
<td style="text-align:center"></td>
<td style="text-align:center">新字符串</td>
<td style="text-align:center">将字符串左侧空白删除</td>
</tr>
<tr>
<td style="text-align:center">.strip()</td>
<td style="text-align:center"></td>
<td style="text-align:center">新字符串</td>
<td style="text-align:center">将字符串两端空白删除</td>
</tr>
<tr>
<td style="text-align:center">str()</td>
<td style="text-align:center">1</td>
<td style="text-align:center">新字符串</td>
<td style="text-align:center">将参数类型转换为字符串</td>
</tr>
<tr>
<td style="text-align:center">.replace(old,new)</td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center">将字符串中old替换成new字符串</td>
</tr>
<tr>
<td style="text-align:center">.split(sep=None)</td>
<td style="text-align:center">分隔符</td>
<td style="text-align:center">拆分后的列表</td>
<td style="text-align:center">用参数作为分隔符拆分字符串返回列表</td>
</tr>
<tr>
<td style="text-align:center">.join(iterable)</td>
<td style="text-align:center">序列</td>
<td style="text-align:center">字符串</td>
<td style="text-align:center">将参数序列用.前的字符作为分隔符拼起来</td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
<h3 id="格式化">格式化<a class="header-anchor" href="#格式化"> ¶ </a></h3>
<h4 id="format格式化">format格式化<a class="header-anchor" href="#format格式化"> ¶ </a></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 位置参数</span></span><br><span class="line"><span class="string">&quot;&#123;0&#125; love &#123;1&#125;&quot;</span>.<span class="built_in">format</span>(<span class="string">&quot;I&quot;</span>, <span class="string">&quot;you&quot;</span>)</span><br><span class="line"><span class="comment"># 关键字参数</span></span><br><span class="line"><span class="string">&quot;&#123;a&#125; love &#123;b&#125;&quot;</span>.<span class="built_in">format</span>(a = <span class="string">&quot;I&quot;</span>, b = <span class="string">&quot;you&quot;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 若混合使用，位置参数必须在关键字参数的前面</span></span><br></pre></td></tr></table></figure>
<h4 id="格式化操作符">%格式化操作符<a class="header-anchor" href="#格式化操作符"> ¶ </a></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="string">&quot;%c&quot;</span> % <span class="number">97</span></span><br><span class="line"><span class="comment"># 类似于C</span></span><br></pre></td></tr></table></figure>
<h2 id="序列">序列<a class="header-anchor" href="#序列"> ¶ </a></h2>
<h3 id="列表">列表<a class="header-anchor" href="#列表"> ¶ </a></h3>
<table>
<thead>
<tr>
<th style="text-align:center">方法/函数</th>
<th style="text-align:center">参数</th>
<th style="text-align:center">返回值</th>
<th style="text-align:center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">list()</td>
<td style="text-align:center">可迭代对象</td>
<td style="text-align:center">列表</td>
<td style="text-align:center">将可迭代对象转换为列表</td>
</tr>
<tr>
<td style="text-align:center">len()</td>
<td style="text-align:center">列表</td>
<td style="text-align:center">列表长度</td>
<td style="text-align:center">返回列表长度</td>
</tr>
<tr>
<td style="text-align:center">max()</td>
<td style="text-align:center">列表</td>
<td style="text-align:center">一个列表元素</td>
<td style="text-align:center">返回列表中最大值</td>
</tr>
<tr>
<td style="text-align:center">min()</td>
<td style="text-align:center">列表</td>
<td style="text-align:center">一个列表元素</td>
<td style="text-align:center">返回列表中最小值</td>
</tr>
<tr>
<td style="text-align:center">.append()</td>
<td style="text-align:center">1</td>
<td style="text-align:center"></td>
<td style="text-align:center">将参数添加到列表末尾</td>
</tr>
<tr>
<td style="text-align:center">.insert()</td>
<td style="text-align:center">1,2</td>
<td style="text-align:center"></td>
<td style="text-align:center">将参数2<strong>插入</strong>到列表下标为参数1的位置</td>
</tr>
<tr>
<td style="text-align:center">del</td>
<td style="text-align:center">1(删除索引或切片)</td>
<td style="text-align:center"></td>
<td style="text-align:center">删除列表元素，用法<code>del alist[1]</code></td>
</tr>
<tr>
<td style="text-align:center">.pop()</td>
<td style="text-align:center">[1]</td>
<td style="text-align:center">删除的元素</td>
<td style="text-align:center">删除末尾元素，或下标为参数1的元素</td>
</tr>
<tr>
<td style="text-align:center">.remove()</td>
<td style="text-align:center">1</td>
<td style="text-align:center"></td>
<td style="text-align:center">删除列表中第一个值为参数1的元素</td>
</tr>
<tr>
<td style="text-align:center">.sort()</td>
<td style="text-align:center">[reverse=True]</td>
<td style="text-align:center"></td>
<td style="text-align:center">将原列表从小到大排序（+可选参数从大到小）</td>
</tr>
<tr>
<td style="text-align:center">sorted()</td>
<td style="text-align:center">1,[reverse=True]</td>
<td style="text-align:center">排序好的列表</td>
<td style="text-align:center">将参数1列表 sort排序</td>
</tr>
<tr>
<td style="text-align:center">.reverse()</td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center">反转列表元素排列顺序</td>
</tr>
<tr>
<td style="text-align:center">.extend()</td>
<td style="text-align:center">1(一个列表)</td>
<td style="text-align:center"></td>
<td style="text-align:center">将参数列表添加到列表末尾</td>
</tr>
<tr>
<td style="text-align:center">+号运算符</td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center">将两个列表合并</td>
</tr>
<tr>
<td style="text-align:center">.count()</td>
<td style="text-align:center">1</td>
<td style="text-align:center"></td>
<td style="text-align:center">统计参数在列表中出现的次数</td>
</tr>
<tr>
<td style="text-align:center">.index()</td>
<td style="text-align:center">1</td>
<td style="text-align:center"></td>
<td style="text-align:center">返回参数在列表中第一次出现的索引值</td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
<h3 id="生成列表">生成列表<a class="header-anchor" href="#生成列表"> ¶ </a></h3>
<p>函数range(start,end,step)：包含start不包括end，步长为step</p>
<h4 id="生成序列、元组">生成序列、元组<a class="header-anchor" href="#生成序列、元组"> ¶ </a></h4>
<p><code>enumerate(iterable)</code>：生成一个二元组，每个二元组分别是可迭代对象的索引号和对应元素</p>
<p><code>zip(iter1,....)</code>：由多个可迭代对象生成多元组</p>
<h5 id="收集参数">收集参数<a class="header-anchor" href="#收集参数"> ¶ </a></h5>
<p>形参前加一个<code>*</code>，可获取任意数量的参数，将参数变为序列。</p>
<p>实参前加一个<code>*</code>，可将序列解包</p>
<h4 id="使用range-创建数字列表">使用range()创建数字列表<a class="header-anchor" href="#使用range-创建数字列表"> ¶ </a></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">numbers = <span class="built_in">list</span>(<span class="built_in">range</span>(<span class="number">1</span>,<span class="number">6</span>))</span><br><span class="line"><span class="built_in">print</span>(numbers)</span><br><span class="line"><span class="comment">###########</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>]</span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">squares = [value**<span class="number">2</span> <span class="keyword">for</span> value <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>,<span class="number">11</span>)]</span><br><span class="line"><span class="built_in">print</span>(squares)</span><br><span class="line"><span class="comment">############</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">4</span>, <span class="number">9</span>, <span class="number">16</span>, <span class="number">25</span>, <span class="number">36</span>, <span class="number">49</span>, <span class="number">64</span>, <span class="number">81</span>, <span class="number">100</span>]</span><br></pre></td></tr></table></figure>
<h4 id="列表的切片">列表的切片<a class="header-anchor" href="#列表的切片"> ¶ </a></h4>
<p>列表切片同 range</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">players = [<span class="string">&#x27;charles&#x27;</span>, <span class="string">&#x27;martina&#x27;</span>, <span class="string">&#x27;michael&#x27;</span>, <span class="string">&#x27;florence&#x27;</span>, <span class="string">&#x27;eli&#x27;</span>] </span><br><span class="line"><span class="built_in">print</span>(players[<span class="number">0</span>:<span class="number">3</span>])</span><br><span class="line"><span class="comment">##############</span></span><br><span class="line">[<span class="string">&#x27;charles&#x27;</span>, <span class="string">&#x27;martina&#x27;</span>, <span class="string">&#x27;michael&#x27;</span>]</span><br></pre></td></tr></table></figure>
<p>负数则倒数开始，-1是倒数第一个</p>
<h4 id="列表的复制">列表的复制<a class="header-anchor" href="#列表的复制"> ¶ </a></h4>
<p>使用切片来复制列表，不要直接用=</p>
<p>因为只是给原来的列表起了两个名字</p>
<h3 id="元组">元组<a class="header-anchor" href="#元组"> ¶ </a></h3>
<p>元组是不能被修改的列表</p>
<p>修改元组只能给元组变量重新赋值</p>
<p><strong>元组的定义是看<code>,</code>逗号，而不是小括号</strong></p>
<p>删除整个元组使用<code>del</code></p>
<p>tuple(ierable) ：将可迭代对象转换为元组</p>
<h3 id="集合">集合<a class="header-anchor" href="#集合"> ¶ </a></h3>
<p>集合 具有唯一性， 会自动删除重复的数据</p>
<h4 id="创建集合">创建集合<a class="header-anchor" href="#创建集合"> ¶ </a></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">set1 = &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">1</span>&#125;</span><br><span class="line">set2 = <span class="built_in">set</span>([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">1</span>])</span><br><span class="line">set1 == set2</span><br><span class="line"><span class="comment"># True</span></span><br></pre></td></tr></table></figure>
<p>使用add()添加元素，remove()移除元素</p>
<h4 id="不可变集合">不可变集合<a class="header-anchor" href="#不可变集合"> ¶ </a></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">set3 = <span class="built_in">frozenset</span>([<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">1</span>])</span><br></pre></td></tr></table></figure>
<h2 id="if">if<a class="header-anchor" href="#if"> ¶ </a></h2>
<p>连环判断else if可以写成elif</p>
<p>python的<code>and</code>对应<code>&amp;&amp;</code>；<code>or</code>对应<code>||</code></p>
<p>确定列表是否为空<code>if alist: #不为空则进入if</code></p>
<h3 id="三元运算符">三元运算符<a class="header-anchor" href="#三元运算符"> ¶ </a></h3>
<p><code>a = x if x&lt;y else y</code></p>
<p>如果if后条件为true，则为x；否则为y</p>
<h2 id="循环">循环<a class="header-anchor" href="#循环"> ¶ </a></h2>
<h3 id="for循环">for循环<a class="header-anchor" href="#for循环"> ¶ </a></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">range</span>(stop)</span><br><span class="line"><span class="built_in">range</span>(start, stop)</span><br><span class="line"><span class="built_in">range</span>(start, stop, step)</span><br></pre></td></tr></table></figure>
<h3 id="while-循环">while 循环<a class="header-anchor" href="#while-循环"> ¶ </a></h3>
<p>else可用于for或者while，表示循环条件不成立时候应执行的内容</p>
<p>当循环有break和else时，break跳出时候并不会执行else内容</p>
<h2 id="字典">字典<a class="header-anchor" href="#字典"> ¶ </a></h2>
<p>字典示例</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">alien_0 = &#123;<span class="string">&#x27;color&#x27;</span>: <span class="string">&#x27;green&#x27;</span>, <span class="string">&#x27;points&#x27;</span>: <span class="number">5</span>&#125;</span><br></pre></td></tr></table></figure>
<p>访问字典</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">print</span>(alien_0[<span class="string">&#x27;color&#x27;</span>])</span><br><span class="line"><span class="comment">#####################</span></span><br><span class="line">green</span><br></pre></td></tr></table></figure>
<p>添加键值对</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 直接添加</span></span><br><span class="line">alien_0[<span class="string">&#x27;x&#x27;</span>] = <span class="number">60</span></span><br></pre></td></tr></table></figure>
<p>创建空字典</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">alien_0 = &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">## 用dict()创建字典</span></span><br><span class="line">d = <span class="built_in">dict</span>(((<span class="string">&#x27;name&#x27;</span>,<span class="string">&quot;Li&quot;</span>),(<span class="string">&quot;sex&quot;</span>,<span class="string">&quot;man&quot;</span>)))</span><br><span class="line"><span class="comment"># d为</span></span><br><span class="line">&#123;<span class="string">&#x27;name&#x27;</span>: <span class="string">&#x27;Li&#x27;</span>, <span class="string">&#x27;sex&#x27;</span>: <span class="string">&#x27;man&#x27;</span>&#125;</span><br></pre></td></tr></table></figure>
<p>修改键值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 直接赋值修改</span></span><br><span class="line">alien_0[<span class="string">&#x27;color&#x27;</span>] = <span class="string">&#x27;red&#x27;</span></span><br></pre></td></tr></table></figure>
<p>删除键值</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 同列表</span></span><br><span class="line"><span class="keyword">del</span> alien_0[<span class="string">&#x27;x&#x27;</span>]</span><br></pre></td></tr></table></figure>
<h3 id="遍历字典">遍历字典<a class="header-anchor" href="#遍历字典"> ¶ </a></h3>
<h4 id="遍历所有的键值对">遍历所有的键值对<a class="header-anchor" href="#遍历所有的键值对"> ¶ </a></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">user_0 = &#123; </span><br><span class="line">    <span class="string">&#x27;username&#x27;</span>: <span class="string">&#x27;efermi&#x27;</span>, </span><br><span class="line">    <span class="string">&#x27;first&#x27;</span>: <span class="string">&#x27;enrico&#x27;</span>, </span><br><span class="line">    <span class="string">&#x27;last&#x27;</span>: <span class="string">&#x27;fermi&#x27;</span>, </span><br><span class="line">    &#125; </span><br><span class="line"> </span><br><span class="line"><span class="keyword">for</span> key, value <span class="keyword">in</span> user_0.items(): </span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;\nKey: &quot;</span> + key) </span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;Value: &quot;</span> + value)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 方法 .items() 返回一个键值对列表</span></span><br></pre></td></tr></table></figure>
<h4 id="遍历所有键">遍历所有键<a class="header-anchor" href="#遍历所有键"> ¶ </a></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> name <span class="keyword">in</span> favorite_languages.keys():</span><br></pre></td></tr></table></figure>
<h4 id="遍历所有值">遍历所有值<a class="header-anchor" href="#遍历所有值"> ¶ </a></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">for</span> name <span class="keyword">in</span> favorite_languages.values():</span><br></pre></td></tr></table></figure>
<h3 id="字典内置方法">字典内置方法<a class="header-anchor" href="#字典内置方法"> ¶ </a></h3>
<ul>
<li><code>.fromkeys()</code>
<ul>
<li>用于创建并返回一个新的字典</li>
<li>第一个参数是字典的键<strong>序列</strong></li>
<li>第二个参数是键的值</li>
<li>会将所有键初始化为一个值</li>
</ul>
</li>
<li><code>.get()</code>
<ul>
<li>用于访问字典项</li>
<li>第一个参数为 需要查找值的 <strong>键</strong></li>
<li>第二个参数可选，如果没有找到返回的字符串</li>
</ul>
</li>
<li><code>.clear()</code>：用于清空字典</li>
<li><code>.copy()</code>：拷贝整个字典</li>
<li><code>.pop(key)</code>：弹出（删除）键</li>
<li><code>.popitem()</code>：弹出一个键值</li>
<li><code>.setdefault()</code>：类似于get查找，但如果查找不到键时候会自动添加进字典</li>
<li><code>.update()</code>：更新字典，例如<code>p.update(name = &quot;LiHua&quot;)</code></li>
</ul>
<p>当形参收集参数是 两个星号<code>**</code>时，参数会被打包成字典</p>
<h2 id="函数">函数<a class="header-anchor" href="#函数"> ¶ </a></h2>
<p>想在函数内修改全局变量，需要使用<code>global</code>关键字</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">count = <span class="number">0</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">fun</span>():</span></span><br><span class="line">    <span class="keyword">global</span> count</span><br><span class="line">    count = <span class="number">5</span></span><br><span class="line">    </span><br></pre></td></tr></table></figure>
<p>想在内部函数修改外部函数的局部变量，使用<code>nonlocal</code>关键字，用法同global</p>
<h3 id="lambda函数">lambda函数<a class="header-anchor" href="#lambda函数"> ¶ </a></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">lambda</span> x,y:x+y</span><br><span class="line"><span class="comment"># :冒号 前为函数的参数，多个参数使用,分割</span></span><br><span class="line"><span class="comment"># 后为 函数的返回值</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 要想调用这个lambda函数，需要绑定一个名字</span></span><br><span class="line">p = <span class="keyword">lambda</span> x, y: x+y</span><br><span class="line">p(<span class="number">2</span>,<span class="number">3</span>)</span><br></pre></td></tr></table></figure>
<h3 id="filter-过滤器">filter() 过滤器<a class="header-anchor" href="#filter-过滤器"> ¶ </a></h3>
<p>两个参数</p>
<ul>
<li>
<p>第一个参数：为函数，则将第二个参数可迭代对象传进去，筛选出函数返回True的值</p>
<p>为None（不可省略），则将第二个参数中 为True的 筛选出来</p>
</li>
</ul>
<p>可结合lambda表达式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">list</span>(<span class="built_in">filter</span>(<span class="keyword">lambda</span> x:x%<span class="number">2</span>,<span class="built_in">range</span>(<span class="number">10</span>)))</span><br><span class="line"><span class="comment"># 输出</span></span><br><span class="line">[<span class="number">1</span>, <span class="number">3</span>, <span class="number">5</span>, <span class="number">7</span>, <span class="number">9</span>]</span><br></pre></td></tr></table></figure>
<h3 id="map">map()<a class="header-anchor" href="#map"> ¶ </a></h3>
<p>map将第二个参数可迭代对象送到第一个参数函数内加工，返回加工后的返回值序列</p>
<p>第二个参数是收集参数，因此第一个参数 函数可以是多参数函数</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">list</span>(<span class="built_in">map</span>(<span class="keyword">lambda</span> x:<span class="number">2</span>*x,<span class="built_in">range</span>(<span class="number">10</span>)))</span><br><span class="line"><span class="comment"># 输出</span></span><br><span class="line">[<span class="number">0</span>, <span class="number">2</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">8</span>, <span class="number">10</span>, <span class="number">12</span>, <span class="number">14</span>, <span class="number">16</span>, <span class="number">18</span>]</span><br></pre></td></tr></table></figure>
<h2 id="文件系统">文件系统<a class="header-anchor" href="#文件系统"> ¶ </a></h2>
<h3 id="打开文件">打开文件<a class="header-anchor" href="#打开文件"> ¶ </a></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">f = <span class="built_in">open</span>(<span class="string">&quot;read.txt&quot;</span>)	<span class="comment"># 以默认只读模式打开</span></span><br></pre></td></tr></table></figure>
<p>mode模式</p>
<table>
<thead>
<tr>
<th style="text-align:left">模式</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">t</td>
<td style="text-align:left">文本模式 (默认)。</td>
</tr>
<tr>
<td style="text-align:left">x</td>
<td style="text-align:left">写模式，新建一个文件，如果该文件已存在则会报错。</td>
</tr>
<tr>
<td style="text-align:left">b</td>
<td style="text-align:left">二进制模式。</td>
</tr>
<tr>
<td style="text-align:left">+</td>
<td style="text-align:left">打开一个文件进行更新(可读可写)。</td>
</tr>
<tr>
<td style="text-align:left">U</td>
<td style="text-align:left">通用换行模式（<strong>Python 3 不支持</strong>）。</td>
</tr>
<tr>
<td style="text-align:left">r</td>
<td style="text-align:left">以<strong>只读方式</strong>打开文件。文件的指针将会放在文件的开头。这是默认模式。</td>
</tr>
<tr>
<td style="text-align:left">rb</td>
<td style="text-align:left">以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。这是默认模式。一般用于非文本文件如图片等。</td>
</tr>
<tr>
<td style="text-align:left">r+</td>
<td style="text-align:left"><strong>打开一个文件用于读写</strong>。文件指针将会放在文件的开头。</td>
</tr>
<tr>
<td style="text-align:left">rb+</td>
<td style="text-align:left">以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。一般用于非文本文件如图片等。</td>
</tr>
<tr>
<td style="text-align:left">w</td>
<td style="text-align:left">打开一个文件<strong>只用于写入</strong>。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。</td>
</tr>
<tr>
<td style="text-align:left">wb</td>
<td style="text-align:left">以二进制格式打开一个文件只用于写入。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。一般用于非文本文件如图片等。</td>
</tr>
<tr>
<td style="text-align:left">w+</td>
<td style="text-align:left">打开一个文件用于读写。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。</td>
</tr>
<tr>
<td style="text-align:left">wb+</td>
<td style="text-align:left">以二进制格式打开一个文件用于读写。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。一般用于非文本文件如图片等。</td>
</tr>
<tr>
<td style="text-align:left">a</td>
<td style="text-align:left">**打开一个文件用于追加。**如果该文件已存在，文件指针将会放在文件的结尾。也就是说，新的内容将会被写入到已有内容之后。如果该文件不存在，创建新文件进行写入。</td>
</tr>
<tr>
<td style="text-align:left">ab</td>
<td style="text-align:left">以二进制格式打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。也就是说，新的内容将会被写入到已有内容之后。如果该文件不存在，创建新文件进行写入。</td>
</tr>
<tr>
<td style="text-align:left">a+</td>
<td style="text-align:left">打开一个文件用于读写。如果该文件已存在，文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在，创建新文件用于读写。</td>
</tr>
<tr>
<td style="text-align:left">ab+</td>
<td style="text-align:left">以二进制格式打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。如果该文件不存在，创建新文件用于读写。</td>
</tr>
</tbody>
</table>
<p>默认为文本模式，如果要以二进制模式打开，加上 <strong>b</strong> 。</p>
<h3 id="file-对象">file 对象<a class="header-anchor" href="#file-对象"> ¶ </a></h3>
<p>file 对象使用 open 函数来创建，下表列出了 file 对象常用的函数：</p>
<table>
<thead>
<tr>
<th style="text-align:left">序号</th>
<th style="text-align:left">方法及描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">1</td>
<td style="text-align:left"><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-file-close.html">file.close()</a>关闭文件。关闭后文件不能再进行读写操作。</td>
</tr>
<tr>
<td style="text-align:left">2</td>
<td style="text-align:left"><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-file-flush.html">file.flush()</a>刷新文件内部缓冲，直接把内部缓冲区的数据立刻写入文件, 而不是被动的等待输出缓冲区写入。</td>
</tr>
<tr>
<td style="text-align:left">3</td>
<td style="text-align:left"><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-file-fileno.html">file.fileno()</a>返回一个整型的文件描述符(file descriptor FD 整型), 可以用在如os模块的read方法等一些底层操作上。</td>
</tr>
<tr>
<td style="text-align:left">4</td>
<td style="text-align:left"><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-file-isatty.html">file.isatty()</a>如果文件连接到一个终端设备返回 True，否则返回 False。</td>
</tr>
<tr>
<td style="text-align:left">5</td>
<td style="text-align:left"><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-file-next.html">file.next()</a>**Python 3 中的 File 对象不支持 next() 方法。**返回文件下一行。</td>
</tr>
<tr>
<td style="text-align:left">6</td>
<td style="text-align:left"><code>file.read([size])</code>从文件读取指定的字节数，<strong>如果未给定或为负则读取所有</strong>。</td>
</tr>
<tr>
<td style="text-align:left">7</td>
<td style="text-align:left"><code>file.readline([size])</code>读取整行，包括 “\n” 字符。</td>
</tr>
<tr>
<td style="text-align:left">8</td>
<td style="text-align:left"><code>file.readlines([sizeint])</code>读取所有行并返回列表，若给定sizeint&gt;0，返回总和大约为sizeint字节的行, 实际读取值可能比 sizeint 较大, 因为需要填充缓冲区。</td>
</tr>
<tr>
<td style="text-align:left">9</td>
<td style="text-align:left"><code>file.seek(offset[, whence])</code>移动文件读取指针到指定位置</td>
</tr>
<tr>
<td style="text-align:left">10</td>
<td style="text-align:left"><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-file-tell.html">file.tell()</a>返回文件当前位置。</td>
</tr>
<tr>
<td style="text-align:left">11</td>
<td style="text-align:left"><code>file.truncate([size])</code>从文件的首行首字符开始截断，截断文件为 size 个字符，无 size 表示从当前位置截断；截断之后后面的所有字符被删除，其中 windows 系统下的换行代表2个字符大小。</td>
</tr>
<tr>
<td style="text-align:left">12</td>
<td style="text-align:left"><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-file-write.html">file.write(str)</a>将字符串写入文件，返回的是写入的字符长度。</td>
</tr>
<tr>
<td style="text-align:left">13</td>
<td style="text-align:left"><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-file-writelines.html">file.writelines(sequence)</a>向文件写入一个序列字符串列表，如果需要换行则要自己加入每行的换行符。</td>
</tr>
</tbody>
</table>
<ul>
<li><code>.tell()</code>：当前文件指针的位置</li>
</ul>
<h3 id="OS模块">OS模块<a class="header-anchor" href="#OS模块"> ¶ </a></h3>
<p><a target="_blank" rel="noopener" href="https://www.runoob.com/python3/python3-os-file-methods.html">菜鸟教程OS模块</a></p>
<h3 id="pickle-保存数据">pickle 保存数据<a class="header-anchor" href="#pickle-保存数据"> ¶ </a></h3>
<p>pickling：将python的对象转换为二进制数据保存；unpickling：从二进制文件中读取python对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> pickle</span><br><span class="line">alist = [<span class="number">1</span>, <span class="number">3.14</span>, <span class="string">&quot;You&quot;</span>]</span><br><span class="line">file = <span class="built_in">open</span>(<span class="string">&quot;alist.pkl&quot;</span>,<span class="string">&quot;wb&quot;</span>)</span><br><span class="line">pickle.dump(alist, file)</span><br><span class="line">file.close()</span><br><span class="line"><span class="comment">###############</span></span><br><span class="line"></span><br><span class="line"><span class="comment">############### 读取</span></span><br><span class="line"><span class="keyword">import</span> pickle</span><br><span class="line">file = <span class="built_in">open</span>(<span class="string">&quot;alist.pkl&quot;</span>,<span class="string">&quot;rb&quot;</span>)</span><br><span class="line">alist = pickle.load(file)</span><br><span class="line">file.close()</span><br></pre></td></tr></table></figure>
<h2 id="异常处理">异常处理<a class="header-anchor" href="#异常处理"> ¶ </a></h2>
<h3 id="捕获异常">捕获异常<a class="header-anchor" href="#捕获异常"> ¶ </a></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="comment"># 检测范围</span></span><br><span class="line"><span class="keyword">except</span> Exception [<span class="keyword">as</span> reason]:	<span class="comment"># 后可省略，或者引用具体错误信息reason</span></span><br><span class="line">    <span class="comment"># 异常处理语句</span></span><br><span class="line"><span class="keyword">except</span> TypeError:</span><br><span class="line">    <span class="comment"># ...</span></span><br><span class="line"><span class="keyword">finally</span>:</span><br><span class="line">    <span class="comment"># 无论是否出现异常都会被执行的内容</span></span><br></pre></td></tr></table></figure>
<h3 id="抛出异常">抛出异常<a class="header-anchor" href="#抛出异常"> ¶ </a></h3>
<p>使用<code>raise</code>关键字抛出异常</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">raise</span> ZeroDivisionError(<span class="string">&quot;除数不嫩那个为零！&quot;</span>) <span class="comment"># 括号为对异常的解释，可省略</span></span><br></pre></td></tr></table></figure>
<h3 id="else">else<a class="header-anchor" href="#else"> ¶ </a></h3>
<p>只要try语句块没有出现任何异常，就会执行else语句块的内容</p>
<h3 id="with语句">with语句<a class="header-anchor" href="#with语句"> ¶ </a></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">try</span>:</span><br><span class="line">    <span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&quot;read.txt&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">        <span class="keyword">for</span> each_line <span class="keyword">in</span> f:</span><br><span class="line">            <span class="built_in">print</span>(each_line)</span><br><span class="line"><span class="keyword">except</span> OSError <span class="keyword">as</span> reason:</span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;出错啦：&quot;</span>  + <span class="built_in">str</span>(reason))</span><br></pre></td></tr></table></figure>
<h2 id="类和对象">类和对象<a class="header-anchor" href="#类和对象"> ¶ </a></h2>
<h3 id="定义一个类">定义一个类<a class="header-anchor" href="#定义一个类"> ¶ </a></h3>
<p><code>__init__</code>为构造方法，定义这个类的属性，<strong>第一个参数为self（类似this）</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Ball</span>:</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init_</span>(<span class="params">self, name</span>):</span></span><br><span class="line">        self.name = name</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">setName</span>(<span class="params">self, name</span>):</span></span><br><span class="line">        self.name = name</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">kick</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&quot;我叫%s,谁踢我?!&quot;</span> % self.name)</span><br></pre></td></tr></table></figure>
<h3 id="公有、私有">公有、私有<a class="header-anchor" href="#公有、私有"> ¶ </a></h3>
<p>python默认公有，定义私有属性需要在  变量名或者函数名前加上<code>__</code>两个下划线，就会变成私有的了。</p>
<h3 id="类的继承">类的继承<a class="header-anchor" href="#类的继承"> ¶ </a></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 语法</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> 类名(<span class="params">被继承的类</span>):</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#### 多重继承</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> 子类(<span class="params">父类<span class="number">1</span>, 父类<span class="number">2</span>, ....</span>):</span>	<span class="comment"># 多个父类使用,分隔</span></span><br></pre></td></tr></table></figure>
<p>子类定义与父类同名的方法或者属性，则会自动覆盖父类对应的方法或者属性（方法重写）</p>
<p>子类在构造方法中需要写父类的构造方法</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Shark</span>(<span class="params">Fish</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">        Fish.__init__(self)</span><br><span class="line">        self.hungry = <span class="literal">True</span></span><br><span class="line"></span><br><span class="line"><span class="comment">## 或者使用super(推荐)</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Shark</span>(<span class="params">Fish</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="built_in">super</span>.__init__()</span><br><span class="line">        self.hungry = <span class="literal">True</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h3 id="类的组合">类的组合<a class="header-anchor" href="#类的组合"> ¶ </a></h3>
<p>一个类中的属性是另一个类</p>
<h3 id="类对象和实例对象">类对象和实例对象<a class="header-anchor" href="#类对象和实例对象"> ¶ </a></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">C</span>:</span></span><br><span class="line">    count = <span class="number">0</span></span><br><span class="line">a = C()</span><br><span class="line">b = C()</span><br><span class="line">c = C()</span><br><span class="line"><span class="built_in">print</span>(a.count, b.count, c.count)	<span class="comment"># 0 0 0</span></span><br><span class="line">c.count += <span class="number">10</span></span><br><span class="line"><span class="built_in">print</span>(a.count, b.count, c.count)	<span class="comment"># 0 0 10</span></span><br><span class="line">C.count += <span class="number">100</span></span><br><span class="line"><span class="built_in">print</span>(a.count, b.count, c.count)	<span class="comment"># 100 100 10</span></span><br></pre></td></tr></table></figure>
<p>当对实例对象c的属性进行赋值时，会覆盖类对象的属性；如果没有赋值，则引用类对象的属性</p>
<h3 id="绑定">绑定<a class="header-anchor" href="#绑定"> ¶ </a></h3>
<p>类方法第一个参数要传<code>self</code></p>
<h3 id="类和对象-BIF-内置函数-：">类和对象 BIF(内置函数)：<a class="header-anchor" href="#类和对象-BIF-内置函数-："> ¶ </a></h3>
<table>
<thead>
<tr>
<th style="text-align:center">BIF</th>
<th style="text-align:center">用途</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>issubclass(class,classinfo)</code></td>
<td style="text-align:center">如果class是classinfo的子类则返回True</td>
</tr>
<tr>
<td style="text-align:center"><code>isinstance(object,classinfo)</code></td>
<td style="text-align:center">如果object是classinfo的[子类]实例对象 返回True</td>
</tr>
<tr>
<td style="text-align:center"><code>hasattr(object, name)</code></td>
<td style="text-align:center">若object对象含有name属性，返回True</td>
</tr>
<tr>
<td style="text-align:center"><code>getattr(object, name[,default])</code></td>
<td style="text-align:center">返回对象指定的属性值，若属性不存在返回default的值/default不存在AttributeError异常</td>
</tr>
<tr>
<td style="text-align:center"><code>setattr(object,name,value)</code></td>
<td style="text-align:center">设置对象中指定属性的值，指定属性不存在则新建属性</td>
</tr>
<tr>
<td style="text-align:center"><code>delattr(object,name)</code></td>
<td style="text-align:center">删除对象指定属性，属性不存在抛出AttributeError异常</td>
</tr>
<tr>
<td style="text-align:center">property()</td>
<td style="text-align:center">给属性绑定getter setter方法</td>
</tr>
</tbody>
</table>
<p>classinfo可以为 类对象组成的元组， 只要其中有一个类来符合条件 就可</p>
<h2 id="魔法方法">魔法方法<a class="header-anchor" href="#魔法方法"> ¶ </a></h2>
<h3 id="注意">注意<a class="header-anchor" href="#注意"> ¶ </a></h3>
<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/zhouyixian/p/11129347.html" title="魔法方法">魔法方法</a></p>
<ul>
<li>魔法方法总是被左右各两个下画线包围，例如_ _init_ _()</li>
<li>魔法方法是面向对象的Python的一切</li>
</ul>
<h3 id="构造和析构">构造和析构<a class="header-anchor" href="#构造和析构"> ¶ </a></h3>
<h4 id="init-self-…">__init__(self[,…])<a class="header-anchor" href="#init-self-…"> ¶ </a></h4>
<ul>
<li>
<p><strong>相当于</strong>构造方法</p>
</li>
<li>
<p><code>_ _init_ _()</code>方法的<strong>返回值一定是None</strong></p>
</li>
<li>
<p>只有在需要进行初始化的时候才重写</p>
</li>
<li>
<p><code>_ _init_ _()</code>并<strong>不是</strong>实例化对象时<strong>第一个被调用</strong>的魔法方法</p>
</li>
</ul>
<h4 id="new-cls-…">__new__(cls[, …])<a class="header-anchor" href="#new-cls-…"> ¶ </a></h4>
<ul>
<li>在一个对象实例化的时候<strong>调用的第一个方法</strong></li>
<li>第一个参数不是self而是这个类(cls)，而其他的参数会直接传递给_ _init_ _()方法</li>
<li>返回一个实例对象，通常是cls这个类实例化的对象，当然也可以返回其他对象</li>
<li>当继承一个不可变的类型的时候，需要重写这个方法</li>
</ul>
<h4 id="del">__del__<a class="header-anchor" href="#del"> ¶ </a></h4>
<ul>
<li>只要在程序没有退出的时候，就执行了<code> del 对象名</code>，就会执行对象对应的类中的del方法</li>
<li><code>_ _del_ _()</code>方法是当垃圾回收机制回收这个对象的时候调用的</li>
</ul>
<h3 id="算数运算">算数运算<a class="header-anchor" href="#算数运算"> ¶ </a></h3>
<ul>
<li>python2.2后将int()、float()、str()、list()、tuple()这些BIF转换为工厂函数<code>&lt;class 'type'&gt;</code>
<ul>
<li>普通bif是<code>&lt;class 'builtin_function_or_method'&gt;</code></li>
</ul>
</li>
<li><a target="_blank" rel="noopener" href="https://www.cnblogs.com/generaotr/p/10565320.html" title="工厂函数">工厂函数理解</a>，<a target="_blank" rel="noopener" href="https://www.zhihu.com/question/20670869" title="工厂函数">工厂函数</a></li>
</ul>
<h4 id="常见算术运算">常见算术运算<a class="header-anchor" href="#常见算术运算"> ¶ </a></h4>
<h4 id="反运算">反运算<a class="header-anchor" href="#反运算"> ¶ </a></h4>
<ul>
<li>反运算魔法方法，一定要注意顺序问题</li>
</ul>
<h4 id="一元操作符">一元操作符<a class="header-anchor" href="#一元操作符"> ¶ </a></h4>
<h3 id="定制">定制<a class="header-anchor" href="#定制"> ¶ </a></h3>
<ul>
<li><strong>如果类中的方法名和属性同名，属性会覆盖方法</strong></li>
</ul>
<h3 id="属性访问">属性访问<a class="header-anchor" href="#属性访问"> ¶ </a></h3>
<ul>
<li>
<p>通过点（.）操作符去访问对象的属性</p>
<ul>
<li>还有bif：getattr，setattr，delattr</li>
<li>property()函数</li>
</ul>
</li>
<li>
<p>用<code>super().</code>可以有效解决死循环</p>
</li>
</ul>
<h3 id="描述符（property原理）">描述符（property原理）<a class="header-anchor" href="#描述符（property原理）"> ¶ </a></h3>
<ul>
<li>
<p>描述符就是将某种特殊类型的类的实例指派给另一个类的属性</p>
<ul>
<li>特殊类型的类指的是：至少要在这个类里边定义<code>_ _get_ _()</code>、<code>__set_ _()</code>或<code>__delete_ _()</code>三个特殊方法中的<strong>任意一个</strong></li>
</ul>
</li>
<li>
<p>与其类似的<a target="_blank" rel="noopener" href="https://www.runoob.com/python/python-func-property.html" title="property">property()</a></p>
</li>
</ul>
<h3 id="定制序列">定制序列<a class="header-anchor" href="#定制序列"> ¶ </a></h3>
<ul>
<li>
<p>容器包括：</p>
<ul>
<li>序列类型：如列表、元组、字符串</li>
<li>映射类型：如字典</li>
</ul>
</li>
<li>
<p>定制容器的有关协议：</p>
<ul>
<li>如果希望定制的容器不可变，则只需要定义<code>_ _len_ _()</code>和<code>_ _getitem_ _()</code>方法</li>
<li>如果希望定制的容器是可变的，除了<code>_ _len_ _()</code>和<code>_ _getitem_ _()</code>方法，还需要定义<code>_ _setitem_ _()</code>和<code>_ _delitem_ _()</code>两个方法</li>
</ul>
</li>
<li>
<p>定制容器相关的魔法方法：</p>
</li>
</ul>
<h3 id="迭代器">迭代器<a class="header-anchor" href="#迭代器"> ¶ </a></h3>
<ul>
<li>
<p>提供迭代方法的容器称为迭代器，通常接触的迭代器有序列（如列表、元组、字符）、字典等，它们都支持迭代的操作</p>
</li>
<li>
<p>对一个容器对象调用iter()就得到它的迭代器。调用next()迭代器就会返回下一个值，如果迭代器没有值可以返回了Python会抛出一个名为StopIteration的异常。</p>
</li>
</ul>
<h3 id="生成器">生成器<a class="header-anchor" href="#生成器"> ¶ </a></h3>
<ul>
<li>
<p>仅通过普通函数，不涉及魔法方法</p>
</li>
<li>
<p>Python是通过生成器来实现类似于协同程序的概念：生成器可以暂时挂起函数，并保留函数的局部变量等数据，然后再次调用它的时候，从上次暂停的位置继续执行下去。</p>
</li>
<li>
<p>yield语句</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">myGen</span>():</span></span><br><span class="line">    <span class="built_in">print</span>(<span class="string">&quot;生成器被执行&quot;</span>)</span><br><span class="line">    <span class="keyword">yield</span> <span class="number">1</span></span><br><span class="line">    <span class="keyword">yield</span> <span class="number">2</span></span><br><span class="line"></span><br><span class="line">myG = myGen()</span><br><span class="line">&gt;&gt;&gt;<span class="built_in">next</span>(myG) <span class="comment">### 生成器被执行</span></span><br><span class="line">			<span class="comment">### 1</span></span><br><span class="line">&gt;&gt;&gt;<span class="built_in">next</span>(myG) <span class="comment">### 2 </span></span><br></pre></td></tr></table></figure>
</li>
<li>
<p>由于Python的for循环会自动调用next()方法和处理StopIteration异常，所以for循环当然也是可以对生成器产生作用</p>
</li>
</ul>
<h3 id="生成器表达式">生成器表达式<a class="header-anchor" href="#生成器表达式"> ¶ </a></h3>
<ul>
<li>
<p>列表推导式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> [i*i <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>)]</span><br><span class="line"><span class="number">2.</span> [i <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>) <span class="keyword">if</span> <span class="keyword">not</span>(i%<span class="number">2</span>) <span class="keyword">and</span> i%<span class="number">3</span>]</span><br></pre></td></tr></table></figure>
</li>
<li>
<p>字典推导式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>&#123;i:i % <span class="number">2</span> == <span class="number">0</span> <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>)&#125;</span><br><span class="line">&#123;<span class="number">0</span>: <span class="literal">True</span>, <span class="number">1</span>: <span class="literal">False</span>, <span class="number">2</span>: <span class="literal">True</span>, <span class="number">3</span>: <span class="literal">False</span>, <span class="number">4</span>: <span class="literal">True</span>, <span class="number">5</span>: <span class="literal">False</span>, <span class="number">6</span>: <span class="literal">True</span>, <span class="number">7</span>: <span class="literal">False</span>,<span class="number">8</span>: <span class="literal">True</span>, <span class="number">9</span>: <span class="literal">False</span>&#125;</span><br></pre></td></tr></table></figure>
</li>
<li>
<p>集合推导式</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&gt;&gt;&gt; </span>&#123;i <span class="keyword">for</span> i <span class="keyword">in</span> [<span class="number">1</span>, <span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">5</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">7</span>, <span class="number">8</span>]&#125;</span><br><span class="line">&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>, <span class="number">7</span>, <span class="number">8</span>&#125;</span><br></pre></td></tr></table></figure>
</li>
</ul>
<h2 id="模块-2">模块<a class="header-anchor" href="#模块-2"> ¶ </a></h2>
<h3 id="导入模块">导入模块<a class="header-anchor" href="#导入模块"> ¶ </a></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> modelName</span><br><span class="line"><span class="keyword">import</span> modelName <span class="keyword">as</span> newName</span><br><span class="line"><span class="keyword">from</span> modelName <span class="keyword">import</span> *</span><br></pre></td></tr></table></figure>
<h3 id="name-属性"><code>__name__</code>属性<a class="header-anchor" href="#name-属性"> ¶ </a></h3>
<p>作为程序运行时候，py程序的<code>__name__</code>属性的值是<code>'__main__'</code></p>
<p>作为模块被导入时，这个值为模块的名字</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 作为模块被导入的时候不会执行，作为程序的时候会执行</span></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<h2 id="爬虫">爬虫<a class="header-anchor" href="#爬虫"> ¶ </a></h2>
<h3 id="urllib模块">urllib模块<a class="header-anchor" href="#urllib模块"> ¶ </a></h3>
<table>
<thead>
<tr>
<th style="text-align:center">模块函数</th>
<th style="text-align:center">参数/返回值</th>
<th style="text-align:center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>urllib.request.urlopen()</code></td>
<td style="text-align:center">参数为url字符串或者request对象，返回类文件对象</td>
<td style="text-align:center">访问网址</td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
<p>爬取的都是以utf-8编码的bytes对象</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line">response = urllib.request.urlopen(<span class="string">&quot;https://www.baidu.com&quot;</span>)</span><br><span class="line">html = response.read()		<span class="comment"># urlopen返回的类文件对象可以直接用文件方法read()</span></span><br></pre></td></tr></table></figure>
<p>示例：下载图片</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># coding=utf-8</span></span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line">img_obj = urllib.request.Request(<span class="string">&quot;http://placekitten.com/800/800&quot;</span>)</span><br><span class="line">img_reponse = urllib.request.urlopen(img_obj)</span><br><span class="line"><span class="keyword">with</span> <span class="built_in">open</span>(<span class="string">&quot;./cat.jpg&quot;</span>, <span class="string">&quot;wb&quot;</span>) <span class="keyword">as</span> f:</span><br><span class="line">    jpg = img_reponse.read()</span><br><span class="line">    f.write(jpg)</span><br></pre></td></tr></table></figure>
<h3 id="request库">request库<a class="header-anchor" href="#request库"> ¶ </a></h3>
<ul>
<li>
<p>r.text是服务器响应的内容，会自动根据响应头部的字符编码进行解码。</p>
</li>
<li>
<p>r.content也是相应返回的字节码</p>
</li>
<li>
<p>r.encoding是服务器内容使用的文本编码。</p>
</li>
<li>
<p>r.status_code用于检测响应的状态码，如果返回200，就表示请求成功了;如果返回的是4xx，就表示客户端错误;返回5xx则表示服务器错误响应。我们可以用r.status_code来检测请求是否正确响应。</p>
</li>
<li>
<p>使用代理，需配置get()参数<code>proxies</code></p>
<ul>
<li>
<pre><code class="language-python">proxies = &#123;
    'http' : address,
    'https' : address,
&#125;
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 保存cookie，创建一个session对象， 使用session对象来进行get和post</span><br><span class="line"></span><br><span class="line">  - ```python</span><br><span class="line">    session = requests.Session()</span><br></pre></td></tr></table></figure>

</code></pre>
</li>
</ul>
</li>
<li></li>
</ul>
<h3 id="Xpath模块">Xpath模块<a class="header-anchor" href="#Xpath模块"> ¶ </a></h3>
<ul>
<li>安装 <code>python -m pip install lxml</code></li>
<li>使用时需导入 <code>from lxml import etree</code></li>
</ul>
<p>学习语法：<a target="_blank" rel="noopener" href="https://www.runoob.com/xpath/xpath-tutorial.html">菜鸟教程 Xpath教程</a></p>
<ul>
<li>提取标签内容使用<code>/text()</code></li>
<li>提取标签属性的属性值使用<code>/@attrname</code></li>
</ul>
<p>部分语法</p>
<table>
<thead>
<tr>
<th style="text-align:center">表达式</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">nodename</td>
<td style="text-align:left">选取此节点的所有子节点。</td>
</tr>
<tr>
<td style="text-align:center">/</td>
<td style="text-align:left">从根节点选取。</td>
</tr>
<tr>
<td style="text-align:center">//</td>
<td style="text-align:left">从匹配选择的当前节点选择文档中的节点，而不考虑它们的位置。</td>
</tr>
<tr>
<td style="text-align:center">.</td>
<td style="text-align:left">选取当前节点。</td>
</tr>
<tr>
<td style="text-align:center">…</td>
<td style="text-align:left">选取当前节点的父节点。</td>
</tr>
<tr>
<td style="text-align:center">@</td>
<td style="text-align:left">选取属性。</td>
</tr>
<tr>
<td style="text-align:center"><code>[starts-with(@attr,value)]</code></td>
<td style="text-align:left">筛选出 标签属性值前半部分相同的</td>
</tr>
<tr>
<td style="text-align:center"><code>string(.)</code></td>
<td style="text-align:left">提取标签里所有的文字</td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:left"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:left"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:left"></td>
</tr>
</tbody>
</table>
<h4 id="示例">示例<a class="header-anchor" href="#示例"> ¶ </a></h4>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> lxml <span class="keyword">import</span> etree</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">test</span>():</span></span><br><span class="line">    t_response = requests.get(<span class="string">&quot;http://www.zdqx.com/pcbz/&quot;</span>)</span><br><span class="line">    t_html = t_response.text</span><br><span class="line">    <span class="built_in">print</span>(t_response.encoding)</span><br><span class="line">    selector = etree.HTML(t_html.encode(<span class="string">&#x27;ISO-8859-1&#x27;</span>).decode())</span><br><span class="line">    result = selector.xpath(<span class="string">&#x27;//ul[@class=&quot;desklist hauto&quot;]/li/a/p/text()&#x27;</span>)</span><br><span class="line">    <span class="keyword">for</span> i <span class="keyword">in</span> result:</span><br><span class="line">        <span class="built_in">print</span>(i)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    test()</span><br></pre></td></tr></table></figure>
<ul>
<li>如果出现中文乱码，自行查看网页编码进行转码，同上面例子</li>
<li>其他例子
<ul>
<li><code>/html/head/title</code>：选择HTML文档中head元素内的title元素。</li>
<li><code>/html/head/title/text()</code>：选择上面提到的title元素的文本。</li>
<li><code>//td</code>：选择所有的td元素。</li>
<li><code>//div[@class=&quot;mine&quot;]</code>：选择所有具有class=&quot;mine&quot;属性的div元<br>
素。</li>
</ul>
</li>
<li>xpath 返回的是列表，列表元素是Selector类型的对象</li>
<li><code>.extract()</code><strong>可以将Selector对象中</strong>data参数存储<strong>的字符串提取出来</strong></li>
</ul>
<h3 id="Python并行化">Python并行化<a class="header-anchor" href="#Python并行化"> ¶ </a></h3>
<p>简单示例</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> multiprocessing  <span class="comment"># 并行化需要导入的库</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">printf</span>(<span class="params">i</span>):</span></span><br><span class="line">    <span class="built_in">print</span>(i)    <span class="comment"># 可迭代对象的处理，尽量不要用输出，输出会乱</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    pool = multiprocessing.Pool(processes=<span class="number">4</span>)    <span class="comment"># 并行化对象pool的声明，参数为进程数，与电脑核数有关</span></span><br><span class="line">    l = [i <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">10</span>)]</span><br><span class="line">    pool.<span class="built_in">map</span>(printf, l)     <span class="comment"># 使用.map方法来对可迭代对象来进行参数一函数的处理</span></span><br><span class="line">    pool.close()        <span class="comment"># 关闭，执行完close后不会有新的进程加入到pool</span></span><br><span class="line">    pool.join()         <span class="comment"># join函数等待所有子进程结束</span></span><br></pre></td></tr></table></figure>
<h2 id="正则表达式">正则表达式<a class="header-anchor" href="#正则表达式"> ¶ </a></h2>
<h3 id="re模块">re模块<a class="header-anchor" href="#re模块"> ¶ </a></h3>
<p>使用前需要导入模块 <code>import re</code></p>
<table>
<thead>
<tr>
<th style="text-align:center">方法</th>
<th style="text-align:center">作用</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>re.sarch(正则，字符串)</code></td>
<td style="text-align:center">从字符串中找正则匹配返回 一个匹配对象</td>
</tr>
<tr>
<td style="text-align:center"><code>re.compile(正则字符串)</code></td>
<td style="text-align:center">将正则编译成模式对象</td>
</tr>
<tr>
<td style="text-align:center"><code>匹配对象.group()</code></td>
<td style="text-align:center">获得匹配对象匹配的字符串</td>
</tr>
<tr>
<td style="text-align:center"><code>模式对象.findall()</code></td>
<td style="text-align:center">包含子组则返回子组组成的列表或者元组</td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
<h3 id="正则知识">正则知识<a class="header-anchor" href="#正则知识"> ¶ </a></h3>
<h4 id="字符类">字符类<a class="header-anchor" href="#字符类"> ¶ </a></h4>
<p>表示一个字符的范围，可以创建一个字符类。</p>
<p>使用中括号将任何内容包起来就是一个字符类；（大小写敏感，可关闭）含义：匹配这个字符类的任何字符就算匹配</p>
<ul>
<li>可使用<code>-</code>小斜杠来表示范围
<ul>
<li>匹配小写字母<code>[a-z]</code></li>
</ul>
</li>
<li>也可直接输入：<code>[abcde12345]</code>：匹配abcde12345中的任何一个字符</li>
</ul>
<h4 id="重复匹配">重复匹配<a class="header-anchor" href="#重复匹配"> ¶ </a></h4>
<p>使用大括号这对元字符来实现重复匹配的功能</p>
<ul>
<li>匹配 3 次：<code>[a-z]&#123;3&#125;</code></li>
<li>匹配3次或者4次或者5次（3到5次）：<code>[a-z]&#123;3,5&#125;</code></li>
</ul>
<h4 id="元字符">元字符<a class="header-anchor" href="#元字符"> ¶ </a></h4>
<ul>
<li>
<p><code>^</code>：匹配字符串的开始位置</p>
</li>
<li>
<p><code>$</code>：匹配字符串的结束位置</p>
</li>
<li>
<p><code>\</code>：</p>
<ul>
<li>将普通字符变成特殊(转义)字符；或者将特殊字符变成普通字符</li>
<li>反斜杠后加数字1~99，表示引用序号对应的子组所匹配的字符串</li>
<li>反斜杠后 0开头或者 三位数字  表示八进制数对应的ASCII字符</li>
</ul>
</li>
<li>
<p>小括号<code>()</code></p>
<p>子组：被 元字符小括号<code>()</code>括起来的正则表达式称为一个子组</p>
<p>例如：<code>(you)\1</code>匹配的是<code>youyou</code></p>
</li>
<li>
<p>中括号<code>[]</code>：生成一个字符类，内部的元字符全都市区了特殊的功能</p>
<ul>
<li><code>-</code>：表示范围</li>
<li><code>/</code>：用于字符串转义：<code>\n</code>匹配换行符</li>
<li><code>^</code>：表示取反</li>
<li><code>&#123;&#125;</code>：表示重复
<ul>
<li>重复2次<code>(you)&#123;2&#125;</code>：匹配<code>youyou</code></li>
<li>重复2~4次<code>(you)&#123;2,4&#125;</code>：匹配<code>youyou</code>，<code>youyouyou</code>，<code>youyouyouyou</code></li>
<li><code>*</code>：相当于<code>&#123;0,&#125;</code>  0次或者无穷多次</li>
<li><code>+</code>：相当于<code>&#123;1,&#125;</code>  匹配至少1次至无穷多次</li>
<li><code>?</code>：相当于<code>&#123;0,1&#125;</code>  匹配0次或者1次</li>
</ul>
</li>
</ul>
</li>
</ul>
<table>
<thead>
<tr>
<th style="text-align:center">元字符</th>
<th style="text-align:center">说明</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">.</td>
<td style="text-align:center">匹配除换行符以外的任意字符</td>
</tr>
<tr>
<td style="text-align:center">\</td>
<td style="text-align:center">将下一个字符标记为一个特殊字符、或一个原义字符、或一个 向后引用、或一个八进制转义符。例如，‘n’ 匹配字符 “n”。‘\n’ 匹配一个换行符。序列 ‘\’ 匹配 “” 而 “(” 则匹配 “(”。</td>
</tr>
<tr>
<td style="text-align:center"><code>^</code></td>
<td style="text-align:center"><code>\A</code></td>
</tr>
<tr>
<td style="text-align:center"><code>$</code></td>
<td style="text-align:center"><code>\Z</code></td>
</tr>
<tr>
<td style="text-align:center">*</td>
<td style="text-align:center">匹配前面的子表达式零次或多次。例如，zo* 能匹配 “z” 以及 “zoo”。* 等价于{0,}。</td>
</tr>
<tr>
<td style="text-align:center">+</td>
<td style="text-align:center">匹配前面的子表达式一次或多次。例如，‘zo+’ 能匹配 “zo” 以及 “zoo”，但不能匹配 “z”。+ 等价于 {1,}。</td>
</tr>
<tr>
<td style="text-align:center">?</td>
<td style="text-align:center">匹配前面的子表达式零次或一次。例如，“do(es)?” 可以匹配 “do” 或 “does” 。? 等价于 {0,1}。</td>
</tr>
<tr>
<td style="text-align:center">{n}</td>
<td style="text-align:center">n 是一个非负整数。匹配确定的 n 次。例如，‘o{2}’ 不能匹配 “Bob” 中的 ‘o’，但是能匹配 “food” 中的两个 o。</td>
</tr>
<tr>
<td style="text-align:center">{n,}</td>
<td style="text-align:center">n 是一个非负整数。至少匹配n 次。例如，‘o{2,}’ 不能匹配 “Bob” 中的 ‘o’，但能匹配 “foooood” 中的所有 o。‘o{1,}’ 等价于 ‘o+’。‘o{0,}’ 则等价于 ‘o*’。</td>
</tr>
<tr>
<td style="text-align:center">{n,m}</td>
<td style="text-align:center">m 和 n 均为非负整数，其中n &lt;= m。最少匹配 n 次且最多匹配 m 次。例如，“o{1,3}” 将匹配 “fooooood” 中的前三个 o。‘o{0,1}’ 等价于 ‘o?’。请注意在逗号和两个数之间不能有空格。</td>
</tr>
<tr>
<td style="text-align:center">?</td>
<td style="text-align:center">当该字符紧跟在任何一个其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 后面时，匹配模式是非贪婪的。非贪婪模式尽可能少的匹配所搜索的字符串，而默认的贪婪模式则尽可能多的匹配所搜索的字符串。例如，对于字符串 “oooo”，‘o+?’ 将匹配单个 “o”，而 ‘o+’ 将匹配所有 ‘o’。</td>
</tr>
<tr>
<td style="text-align:center">.</td>
<td style="text-align:center">匹配除换行符（\n、\r）之外的任何单个字符。要匹配包括 ‘\n’ 在内的任何字符，请使用像&quot;<strong>(.|\n)</strong>&quot;的模式。</td>
</tr>
<tr>
<td style="text-align:center">(pattern)</td>
<td style="text-align:center">匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到，在VBScript 中使用 SubMatches 集合，在JScript 中则使用 $0…$9 属性。要匹配圆括号字符，请使用 ‘(’ 或 ‘)’。</td>
</tr>
<tr>
<td style="text-align:center">(?:pattern)</td>
<td style="text-align:center">匹配 pattern 但不获取匹配结果，也就是说这是一个非获取匹配，不进行存储供以后使用。这在使用 “或” 字符 (|) 来组合一个模式的各个部分是很有用。例如， 'industr(?:y|ies) 就是一个比 ‘industry|industries’ 更简略的表达式。</td>
</tr>
<tr>
<td style="text-align:center">(?=pattern)</td>
<td style="text-align:center">正向肯定预查（look ahead positive assert），在任何匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配，也就是说，该匹配不需要获取供以后使用。例如，“Windows(?=95|98|NT|2000)“能匹配&quot;Windows2000&quot;中的&quot;Windows”，但不能匹配&quot;Windows3.1&quot;中的&quot;Windows”。预查不消耗字符，也就是说，在一个匹配发生后，在最后一次匹配之后立即开始下一次匹配的搜索，而不是从包含预查的字符之后开始。</td>
</tr>
<tr>
<td style="text-align:center">(?!pattern)</td>
<td style="text-align:center">正向否定预查(negative assert)，在任何不匹配pattern的字符串开始处匹配查找字符串。这是一个非获取匹配，也就是说，该匹配不需要获取供以后使用。例如&quot;Windows(?!95|98|NT|2000)“能匹配&quot;Windows3.1&quot;中的&quot;Windows”，但不能匹配&quot;Windows2000&quot;中的&quot;Windows&quot;。预查不消耗字符，也就是说，在一个匹配发生后，在最后一次匹配之后立即开始下一次匹配的搜索，而不是从包含预查的字符之后开始。</td>
</tr>
<tr>
<td style="text-align:center">(?&lt;=pattern)</td>
<td style="text-align:center">反向(look behind)肯定预查，与正向肯定预查类似，只是方向相反。例如，&quot;`(?&lt;=95</td>
</tr>
<tr>
<td style="text-align:center">(?&lt;!pattern)</td>
<td style="text-align:center">反向否定预查，与正向否定预查类似，只是方向相反。例如&quot;`(?&lt;!95</td>
</tr>
<tr>
<td style="text-align:center">x|y</td>
<td style="text-align:center">匹配 x 或 y。例如，‘z|food’ 能匹配 “z” 或 “food”。‘(z|f)ood’ 则匹配 “zood” 或 “food”。</td>
</tr>
<tr>
<td style="text-align:center">[xyz]</td>
<td style="text-align:center">字符集合。匹配所包含的任意一个字符。例如， ‘[abc]’ 可以匹配 “plain” 中的 ‘a’。</td>
</tr>
<tr>
<td style="text-align:center">[^xyz]</td>
<td style="text-align:center">负值字符集合。匹配未包含的任意字符。例如，<code>[^abc]</code> 可以匹配 “plain” 中的’p’、‘l’、‘i’、‘n’。</td>
</tr>
<tr>
<td style="text-align:center">[a-z]</td>
<td style="text-align:center">字符范围。匹配指定范围内的任意字符。例如，<code>[a-z]</code>可以匹配 ‘a’ 到 ‘z’ 范围内的任意小写字母字符。</td>
</tr>
<tr>
<td style="text-align:center">[^a-z]</td>
<td style="text-align:center">负值字符范围。匹配任何不在指定范围内的任意字符。例如，<code>[^a-z]</code> 可以匹配任何不在 ‘a’ 到 ‘z’ 范围内的任意字符。</td>
</tr>
<tr>
<td style="text-align:center">\b</td>
<td style="text-align:center"><strong>匹配一个单词边界</strong>，也就是指单词和空格间的位置。例如， ‘er\b’ 可以匹配&quot;never&quot; 中的 ‘er’，但不能匹配 “verb” 中的 ‘er’。</td>
</tr>
<tr>
<td style="text-align:center">\B</td>
<td style="text-align:center"><strong>匹配非单词边界</strong>。‘er\B’ 能匹配 “verb” 中的 ‘er’，但不能匹配 “never” 中的 ‘er’。</td>
</tr>
<tr>
<td style="text-align:center">\cx</td>
<td style="text-align:center">匹配由 x 指明的控制字符。例如， \cM 匹配一个 Control-M 或回车符。x 的值必须为 A-Z 或 a-z 之一。否则，将 c 视为一个原义的 ‘c’ 字符。</td>
</tr>
<tr>
<td style="text-align:center">\d</td>
<td style="text-align:center">匹配一个数字字符。等价于 [0-9]。</td>
</tr>
<tr>
<td style="text-align:center">\D</td>
<td style="text-align:center">匹配一个非数字字符。等价于 <code>[^0-9]</code>。</td>
</tr>
<tr>
<td style="text-align:center">\f</td>
<td style="text-align:center">匹配一个换页符。等价于 \x0c 和 \cL。</td>
</tr>
<tr>
<td style="text-align:center">\n</td>
<td style="text-align:center">匹配一个换行符。等价于 \x0a 和 \cJ。</td>
</tr>
<tr>
<td style="text-align:center">\r</td>
<td style="text-align:center">匹配一个回车符。等价于 \x0d 和 \cM。</td>
</tr>
<tr>
<td style="text-align:center">\s</td>
<td style="text-align:center">匹配任何空白字符，包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。</td>
</tr>
<tr>
<td style="text-align:center">\S</td>
<td style="text-align:center">匹配任何非空白字符。等价于 <code>[^ \f\n\r\t\v]</code>。</td>
</tr>
<tr>
<td style="text-align:center">\t</td>
<td style="text-align:center">匹配一个制表符。等价于 \x09 和 \cI。</td>
</tr>
<tr>
<td style="text-align:center">\v</td>
<td style="text-align:center">匹配一个垂直制表符。等价于 \x0b 和 \cK。</td>
</tr>
<tr>
<td style="text-align:center">\w</td>
<td style="text-align:center">匹配字母、数字、下划线。等价于<code>[A-Za-z0-9_]</code>。</td>
</tr>
<tr>
<td style="text-align:center">\W</td>
<td style="text-align:center">匹配非字母、数字、下划线。等价于<code> [^A-Za-z0-9_]</code>。</td>
</tr>
<tr>
<td style="text-align:center">\xn</td>
<td style="text-align:center">匹配 n，其中 n 为十六进制转义值。十六进制转义值必须为确定的两个数字长。例如，‘\x41’ 匹配 “A”。‘\x041’ 则等价于 ‘\x04’ &amp; “1”。正则表达式中可以使用 ASCII 编码。</td>
</tr>
<tr>
<td style="text-align:center">\num</td>
<td style="text-align:center">匹配 num，其中 num 是一个正整数。对所获取的匹配的引用。例如，‘(.)\1’ 匹配两个连续的相同字符。</td>
</tr>
<tr>
<td style="text-align:center">\n</td>
<td style="text-align:center">标识一个八进制转义值或一个向后引用。如果 \n 之前至少 n 个获取的子表达式，则 n 为向后引用。否则，如果 n 为八进制数字 (0-7)，则 n 为一个八进制转义值。</td>
</tr>
<tr>
<td style="text-align:center">\nm</td>
<td style="text-align:center">标识一个八进制转义值或一个向后引用。如果 \nm 之前至少有 nm 个获得子表达式，则 nm 为向后引用。如果 \nm 之前至少有 n 个获取，则 n 为一个后跟文字 m 的向后引用。如果前面的条件都不满足，若 n 和 m 均为八进制数字 (0-7)，则 \nm 将匹配八进制转义值 nm。</td>
</tr>
<tr>
<td style="text-align:center">\nml</td>
<td style="text-align:center">如果 n 为八进制数字 (0-3)，且 m 和 l 均为八进制数字 (0-7)，则匹配八进制转义值 nml。</td>
</tr>
<tr>
<td style="text-align:center">\un</td>
<td style="text-align:center">匹配 n，其中 n 是一个用四个十六进制数字表示的 Unicode 字符。例如， \u00A9 匹配版权符号 (?)。</td>
</tr>
</tbody>
</table>
<h3 id="实例">实例<a class="header-anchor" href="#实例"> ¶ </a></h3>
<h3 id="贪婪-和-非贪婪">贪婪 和 非贪婪<a class="header-anchor" href="#贪婪-和-非贪婪"> ¶ </a></h3>
<p>贪婪模式：在 符合的条件下，会尽可能多的去匹配</p>
<p>非贪婪：匹配最少次数</p>
<p>默认为贪婪模式，启用非贪婪模式，在 表示重复的元字符后 加上一个<code>?</code></p>
<h3 id="实践遇到的问题">实践遇到的问题<a class="header-anchor" href="#实践遇到的问题"> ¶ </a></h3>
<ul>
<li>
<p>字符类中匹配<code>-</code>，例如一个字符类匹配<code>abc-</code> 4个字符</p>
<ul>
<li>需要将<code>-</code>写在最前面，如<code>[-abc]</code>，不写在最前面均表示范围</li>
</ul>
</li>
<li>
<p>如何匹配小括号<code>()</code></p>
<ul>
<li>
<p>若匹配的字面值含有<code>()</code>需要将其用中括号括起表示一个字符类</p>
<p>例如：想匹配<code>(</code>需要写成<code>[(]</code></p>
</li>
</ul>
</li>
</ul>
<h2 id="Scrapy-爬虫框架">Scrapy 爬虫框架<a class="header-anchor" href="#Scrapy-爬虫框架"> ¶ </a></h2>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">创建项目：scrapy startproject xxx</span><br><span class="line">进入项目：<span class="built_in">cd</span> xxx #进入某个文件夹下</span><br><span class="line">创建爬虫：scrapy genspider xxx（爬虫名） xxx.com （爬取域）</span><br><span class="line">生成文件：scrapy crawl xxx -o xxx.json (生成某种类型的文件)</span><br><span class="line">运行爬虫：scrapy crawl XXX</span><br><span class="line">列出所有爬虫：scrapy list</span><br><span class="line">获得配置信息：scrapy settings [options]</span><br></pre></td></tr></table></figure>
<h3 id="框架流程">框架流程<a class="header-anchor" href="#框架流程"> ¶ </a></h3>
<h4 id="【1】-创建一个scrpy框架项目">**【1】**创建一个scrpy框架项目<a class="header-anchor" href="#【1】-创建一个scrpy框架项目"> ¶ </a></h4>
<p>cmd命令行输入</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">scrapy startproject [dirname]</span><br></pre></td></tr></table></figure>
<p>会在当前目录生成一个[dirname]的文件夹</p>
<p>该文件夹目录结构为</p>
<ul>
<li>scrapy.cfg：项目的配置文件。</li>
<li>[dirname]/：项目的大本营。</li>
<li>[dirname]/items.py：定义项目中需要抓取并需要后期处理的数据。</li>
<li>[dirname]/middlewares.py：项目中的中间件（自定义扩展下载功能的组件）。</li>
<li>[dirname]/pipelines.py：用于存放执行后期数据处理的功能，从而使得数据的爬取的处理分开</li>
<li>[dirname]/settings.py：项目的设置文件，配置Scrapy。可以设置user-agent，爬取时间间隔，设置代理等等</li>
<li>[dirname]/spiders/：放置爬虫代码的目录。</li>
</ul>
<h5 id="settings-py"><a target="_blank" rel="noopener" href="http://settings.py">settings.py</a><a class="header-anchor" href="#settings-py"> ¶ </a></h5>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># robots协议</span></span><br><span class="line">ROBOTSTXT_OBEY = <span class="literal">True</span> <span class="comment"># / False</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 显示指定类型的日志信息</span></span><br><span class="line">LOG_LEVEL = <span class="string">&#x27;ERROR&#x27;</span>		<span class="comment"># 只显示错误信息，运行时不会太多无关信息</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># User-Agent 头部</span></span><br><span class="line">USER-AGENT = <span class="string">&#x27;&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 开启管道 pipelines.py</span></span><br><span class="line">ITEM_PIPELINES = &#123;</span><br><span class="line">    <span class="string">&#x27;类名&#x27;</span> = <span class="number">300</span>,	<span class="comment"># 数值表示优先级，越小越高</span></span><br><span class="line">    <span class="comment"># 可以在管道里写多个处理类</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment"># 指定图片存储路径</span></span><br><span class="line">IMAGES_STORE = <span class="string">&#x27;./imgs&#x27;</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 开启中间件</span></span><br><span class="line"><span class="comment"># 开启爬虫中间件</span></span><br><span class="line">SPIDER_MIDDLEWARES =&#123;</span><br><span class="line">    <span class="string">&#x27;类名&#x27;</span> = <span class="number">543</span>,	<span class="comment"># 同管道类</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment"># 开启下载中间件</span></span><br><span class="line">DOWNLOADER_MIDDLEWARES =&#123;</span><br><span class="line">    <span class="string">&#x27;类名&#x27;</span> = <span class="number">543</span>,	<span class="comment"># 同管道类</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h5 id="pipelines-py"><a target="_blank" rel="noopener" href="http://pipelines.py">pipelines.py</a><a class="header-anchor" href="#pipelines-py"> ¶ </a></h5>
<p>需要在setting内开启管道配置</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">xxx</span>():</span></span><br><span class="line">    fp = <span class="literal">None</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 重写方法，该方法在开始爬虫前调用一次</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">open_spider</span>(<span class="params">self,spider</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;开始爬虫...&#x27;</span>)</span><br><span class="line">        self.fp = <span class="built_in">open</span>(<span class="string">&#x27;./resu.txt&#x27;</span>,<span class="string">&#x27;w&#x27;</span>,encoding=<span class="string">&#x27;utf-8&#x27;</span>)</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 重写方法 专门用来处理item类型对象</span></span><br><span class="line">    <span class="comment"># 接收spiders提交过来的item对象</span></span><br><span class="line">    <span class="comment"># 每接收到一个item都会被调用一次</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">process_item</span>(<span class="params">self, item, spider</span>):</span></span><br><span class="line">        author = item[<span class="string">&#x27;author&#x27;</span>]</span><br><span class="line">        content = item[<span class="string">&#x27;content&#x27;</span>]</span><br><span class="line">        self.fp.write(author + <span class="string">&#x27;:&#x27;</span> + content + <span class="string">&#x27;\n&#x27;</span>)</span><br><span class="line">        </span><br><span class="line">        <span class="keyword">return</span> item		<span class="comment"># 如果有下一个管道处理类，会将item返回给下一个管道处理类</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 重写方法，结束爬虫后调用一次</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">close_spider</span>(<span class="params">self, spider</span>):</span></span><br><span class="line">        <span class="built_in">print</span>(<span class="string">&#x27;爬虫结束...&#x27;</span>)</span><br><span class="line">        self.fp.close()</span><br></pre></td></tr></table></figure>
<h5 id="middlewares-py"><a target="_blank" rel="noopener" href="http://middlewares.py">middlewares.py</a><a class="header-anchor" href="#middlewares-py"> ¶ </a></h5>
<p>中间件  <strong>使用前需要在settings.py中开启</strong></p>
<ul>
<li>
<p><code>MiddleproSpiderMiddleware(obgect)</code>爬虫中间件</p>
</li>
<li>
<p><code>MiddleproDownloaderMiddleware(obgect)</code>下载中间件</p>
<ul>
<li>
<p><code>from_crawler</code> 返回下载中间件实例化对象</p>
</li>
<li>
<p><code>process_request</code> 用来拦截处理请求</p>
<ul>
<li>
<p>可以写UA伪装</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">request.headers[<span class="string">&#x27;User-Agent&#x27;</span>] = <span class="string">&#x27;&#x27;</span>	<span class="comment"># 可以封装UA池</span></span><br><span class="line">request.meta[<span class="string">&#x27;proxy&#x27;</span>] = <span class="string">&#x27;&#x27;</span>	<span class="comment"># 使用代理</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li>
<p><code>process_response</code> 用于拦截处理响应</p>
</li>
<li>
<p><code>process_exception</code> 用于拦截发生异常请求</p>
<ul>
<li>
<p>一般写代理ip（可以写到request函数）</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 代理池</span></span><br></pre></td></tr></table></figure>
</li>
</ul>
</li>
<li>
<p><code>spider_opend</code> 打印日志</p>
</li>
</ul>
</li>
</ul>
<h4 id="【2】-定义Items-py">**【2】**<a target="_blank" rel="noopener" href="http://xn--Items-xs1hm90f.py">定义Items.py</a><a class="header-anchor" href="#【2】-定义Items-py"> ¶ </a></h4>
<p>Items是将要装载抓取的数据的容器，它工作方式像python里面的字典，但它提供更多的保护，比如对未定义的字段填充以防止拼写错误。</p>
<p>它通过创建一个scrapy.item.Item类来声明，定义它的属性为scrpy.item.Field对象，就像是一个对象关系映射(ORM).<br>
我们通过将需要的item模型化，来控制从dmoz.org获得的站点数据，比如我们要获得站点的名字，url和网站描述，我们定义这三种属性的域。要做到这点，我们编辑在tutorial目录下的items.py文件，我们的Item类将会是这样</p>
<p>示例</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> scrapy.item <span class="keyword">import</span> Item, Field </span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">DmozItem</span>(<span class="params">Item</span>):</span></span><br><span class="line">    title = Field()</span><br><span class="line">    link = Field()</span><br><span class="line">    desc = Field()</span><br></pre></td></tr></table></figure>
<h4 id="【3】-在-dirname-spiders-创建python文件">**【3】**在<code>[dirname]/spiders/</code>创建python文件<a class="header-anchor" href="#【3】-在-dirname-spiders-创建python文件"> ¶ </a></h4>
<p>根目录输入命令，会自动创建</p>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">scrapy genspider [name] [start_url]</span><br></pre></td></tr></table></figure>
<p>在里面编写爬虫类Spider，Spider是用户编写的用于从网站上爬取数据的类</p>
<ul>
<li>创建一个自定义的Spider时，必须继承scrapy.Spider类，且定义以下三个属性：
<ul>
<li>
<p>name：用于<strong>区别不同的Spider</strong>。<strong>该名字必须是唯一</strong>的，不可以为不同的Spider设定相同的名字。</p>
</li>
<li>
<p>start_requests：包含了Spider在启动时进行爬取的URL列表。该函数<strong>必须返回Requests对象</strong>或者其生成器。</p>
<ul>
<li>
<pre><code class="language-python"># Requests对象属性
print(type(response))  # scrapy.http.response.html.HtmlResponse
print(type(response.text))  # str
print(type(response.body))  # bytes
print(response.encoding)  # utf-8
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">  </span><br><span class="line">  - parse()：这是Spider的一个默认回调函数。当下载器返回Response的时候，parse()函数就会被调用，每个初始URL完成下载后生成的Response对象将会作为唯一的参数传递给parse()函数。该方法负责解析返回的数据（response data），提取数据（生成item）以及生成需要进一步处理的URL的Request对象。</span><br><span class="line"></span><br><span class="line">#### **【4】**开始爬取</span><br><span class="line"></span><br><span class="line">在scrapy项目根目录执行命令`scrapy crawl [obj_name]`，其中`obj_name`是python文件自定义类里的name值</span><br><span class="line"></span><br><span class="line">也可以在项目根目录创建`main.py`文件</span><br><span class="line"></span><br><span class="line">```python</span><br><span class="line">from scrapy.cmdline import execute</span><br><span class="line">execute(&quot;scrapy crawl [obj_name]&quot;.split())</span><br></pre></td></tr></table></figure>

</code></pre>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"># 命令行执行</span><br><span class="line">scrapy crawl [obj_name]</span><br><span class="line">scrapy crawl [obj_name] -o ./[file_name].csv</span><br><span class="line">#或其他格式  json csv xml pickle</span><br></pre></td></tr></table></figure>
<h3 id="Scrapy-shell">Scrapy shell<a class="header-anchor" href="#Scrapy-shell"> ¶ </a></h3>
<p>根目录执行命令</p>
<ul>
<li>
<p>加载网页（参数url需要加双引号）</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">scrapy shell url</span><br></pre></td></tr></table></figure>
<p>加载网页成功后，response回应存储在本地变量response中</p>
<ul>
<li><code>response.body</code> ：获得网页html代码</li>
<li><code>response.headers</code> ：获得网页的header部分信息</li>
</ul>
</li>
</ul>
<h3 id="Selector选择器">Selector选择器<a class="header-anchor" href="#Selector选择器"> ¶ </a></h3>
<p>选择器包<code>scrapy.selector.Selector</code></p>
<ul>
<li>response.selector.xpath()：xPath选择器；也可省略写成<code>.xpath()：</code>会返回一系列selectors</li>
<li>response.selector.css()：CSS选择器；也可省略写成<code>.css()</code>：会返回一系列selectors</li>
<li>response.selector.extract()：返回一个unicode字符串，为选中的数据</li>
<li><a target="_blank" rel="noopener" href="http://response.selector.re">response.selector.re</a>()：正则表达式选择器，返回字符串</li>
</ul>
<h3 id="请求传参">请求传参<a class="header-anchor" href="#请求传参"> ¶ </a></h3>
<p>爬取的内容不再同一页面，</p>
<p>用parse函数最后yield scrapy.http.Request来调用一个更深的页面（自己定义函数）</p>
<h3 id="图片爬取">图片爬取<a class="header-anchor" href="#图片爬取"> ¶ </a></h3>
<p>管道类需要继承<code>ImagesPipeLine</code>类</p>
<p>使用前需要先导入包</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> scrapy.pipelines.images <span class="keyword">import</span> ImagesPipeline</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">imgsPipleLine</span>(<span class="params">ImagesPipeline</span>):</span></span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 重写  根据图片地址进行图片数据请求，传入图片src地址</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">get_media_requests</span>(<span class="params">self, item, info</span>):</span></span><br><span class="line">        <span class="keyword">yield</span> scrapy.Request(item[<span class="string">&#x27;src&#x27;</span>])</span><br><span class="line">    </span><br><span class="line">    <span class="comment"># 方法重写 指定图片存储的路径</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">file_path</span>(<span class="params">self, request, response=<span class="literal">None</span>, info=<span class="literal">None</span></span>):</span></span><br><span class="line">        imgName = <span class="string">&#x27;&#x27;</span>	<span class="comment"># 指定每个图片名称</span></span><br><span class="line">        <span class="keyword">return</span> imgName</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">item_completed</span>(<span class="params">self, results, item, info</span>):</span></span><br><span class="line">        <span class="keyword">return</span> item		<span class="comment"># 赶回给下一个即将被执行的管道类</span></span><br></pre></td></tr></table></figure>
<p>需要在<code>settings.py</code>中指定图片存储路径</p>
<h2 id="selenium">selenium<a class="header-anchor" href="#selenium"> ¶ </a></h2>
<p>基于浏览器 自动化测试模块</p>
<p><a target="_blank" rel="noopener" href="https://python-selenium-zh.readthedocs.io/zh_CN/latest/">selenium-python中文文档 (python-selenium-zh.readthedocs.io)</a></p>
<h3 id="安装-2">安装<a class="header-anchor" href="#安装-2"> ¶ </a></h3>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python -m pip install selenium</span><br></pre></td></tr></table></figure>
<h3 id="使用">使用<a class="header-anchor" href="#使用"> ¶ </a></h3>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"><span class="comment"># 4 自定义设置chrome路径，并用chromedriver控制</span></span><br><span class="line">options = webdriver.ChromeOptions()</span><br><span class="line"></span><br><span class="line"><span class="comment">######## 如果想隐藏浏览器窗口（无可视化界面），添加下面两行代码</span></span><br><span class="line">options.add_argument(<span class="string">&#x27;--headless&#x27;</span>)</span><br><span class="line">options.add_argument(<span class="string">&#x27;--disable-gpu&#x27;</span>)</span><br><span class="line"><span class="comment">########</span></span><br><span class="line"></span><br><span class="line"><span class="comment">######## 规避网站检测selenium</span></span><br><span class="line">options.add_experimental_option(<span class="string">&#x27;excludeSwitches&#x27;</span>,[<span class="string">&#x27;enable-automation&#x27;</span>])</span><br><span class="line"><span class="comment">########</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># chrome可执行文件的路径，需要修改chrome文件名</span></span><br><span class="line">options.binary_location = <span class="string">r&#x27;F:\palmer\MiniSoftware\RunningCheeseChrome\App\chrom.exe&#x27;</span></span><br><span class="line">bro = webdriver.Chrome(executable_path=<span class="string">&#x27;./chromedriver.exe&#x27;</span>, chrome_options=options)</span><br><span class="line">bro.get(<span class="string">&#x27;https://www.baidu.com&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>webdriver.Chrome类对象</p>
<table>
<thead>
<tr>
<th style="text-align:center">属性</th>
<th style="text-align:center">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>.page_source</code></td>
<td style="text-align:center">获取浏览器当前页面的html源码数据</td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
<table>
<thead>
<tr>
<th style="text-align:center">方法</th>
<th style="text-align:center">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>.get(url)</code></td>
<td style="text-align:center">打开网址</td>
</tr>
<tr>
<td style="text-align:center"><code>.quit()</code></td>
<td style="text-align:center">关闭浏览器</td>
</tr>
<tr>
<td style="text-align:center"><code>.close()</code></td>
<td style="text-align:center">关闭当前标签</td>
</tr>
<tr>
<td style="text-align:center"><code>.find_element_by_xpath()</code></td>
<td style="text-align:center">使用xpath来寻找元素</td>
</tr>
<tr>
<td style="text-align:center"><code>.find_element_by_id/name()</code></td>
<td style="text-align:center">通过属性值id或者name值来寻找</td>
</tr>
<tr>
<td style="text-align:center"><code>.execute_script()</code></td>
<td style="text-align:center">执行js代码</td>
</tr>
<tr>
<td style="text-align:center"><code>.back()</code></td>
<td style="text-align:center">网页后退</td>
</tr>
<tr>
<td style="text-align:center"><code>.dorward()</code></td>
<td style="text-align:center">网页前进</td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
<p>WebElement类对象</p>
<table>
<thead>
<tr>
<th style="text-align:center">属性/方法</th>
<th style="text-align:center">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center"><code>.get_attribute()</code></td>
<td style="text-align:center">得到标签属性的值</td>
</tr>
<tr>
<td style="text-align:center"><code>.send_keys()</code></td>
<td style="text-align:center">写入内容</td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
<h3 id="定位元素">定位元素<a class="header-anchor" href="#定位元素"> ¶ </a></h3>
<ul>
<li>
<p>根据Id属性值定位：返回<strong>第一个</strong>name属性匹配的元素，如果没有元素匹配，会抛出<code>NoSuchElementException</code>异常。</p>
<ul>
<li>
<pre><code class="language-python">login_form = driver.find_element_by_id('loginForm')
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 根据Name属性值定位：同上</span><br><span class="line"></span><br><span class="line">  - ```python</span><br><span class="line">    continue = driver.find_element_by_name(&#x27;continue&#x27;)</span><br></pre></td></tr></table></figure>

</code></pre>
</li>
</ul>
</li>
<li>
<p>使用XPath定位：</p>
<ul>
<li>
<pre><code class="language-python">clear_button = driver.find_element_by_xpath(&quot;&quot;)
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 使用链接文本定位超链接：</span><br><span class="line"></span><br><span class="line">  - ```python</span><br><span class="line">    continue_link = driver.find_element_by_link_text(&#x27;Continue&#x27;)</span><br><span class="line">    continue_link = driver.find_element_by_partial_link_text(&#x27;Conti&#x27;)</span><br></pre></td></tr></table></figure>

</code></pre>
</li>
<li>
<p>参数即为 a标签内的文字</p>
</li>
</ul>
</li>
<li>
<p>标签名定位</p>
<ul>
<li>
<pre><code class="language-python">heading1 = driver.find_element_by_tag_name('h1')
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- class定位</span><br><span class="line"></span><br><span class="line">  - ```python</span><br><span class="line">    content = driver.find_element_by_class_name(&#x27;content&#x27;)</span><br></pre></td></tr></table></figure>

</code></pre>
</li>
<li>
<p>只返回匹配的第一个</p>
</li>
</ul>
</li>
<li>
<p>css选择器定位</p>
<ul>
<li>
<pre><code class="language-python">content = driver.find_element_by_css_selector('p.content')
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">### 等待事件</span><br><span class="line"></span><br><span class="line">#### 显式等待</span><br><span class="line"></span><br><span class="line">显式的`waits`等待一个确定的条件触发然后才进行更深一步的执行。最糟糕的的做法是`time.sleep()`，这指定的条件是等待一个指定的时间段。 这里提供一些便利的方法让你编写的代码只等待需要的时间，`WebDriverWait`结合`ExpectedCondition`是一种实现的方法：</span><br><span class="line"></span><br><span class="line">```python</span><br><span class="line">from selenium import webdriver</span><br><span class="line">from selenium.webdriver.common.by import By</span><br><span class="line">from selenium.webdriver.support.ui import WebDriverWait</span><br><span class="line">from selenium.webdriver.support import expected_conditions as EC</span><br><span class="line"></span><br><span class="line">driver = webdriver.Firefox()</span><br><span class="line">driver.get(&quot;http://somedomain/url_that_delay_loading&quot;)</span><br><span class="line">try:</span><br><span class="line">    element = WebDriverWait(driver,10).until(</span><br><span class="line">        EC.presence_of_element_located((By.ID,&quot;myDynamicElement&quot;))</span><br><span class="line">    )</span><br><span class="line">finally:</span><br><span class="line">    driver.quit()</span><br></pre></td></tr></table></figure>

</code></pre>
</li>
</ul>
</li>
</ul>
<p>这段代码会等待10秒，如果10秒内找到元素则立即返回，否则会抛出<code>TimeoutException</code>异常，WebDriverWait默认每500毫秒调用一下<code>ExpectedCondition</code>直到它返回成功为止。<code>ExpectedCondition</code>类型是布尔的，成功的返回值就是true,其他类型的<code>ExpectedCondition</code>成功的返回值就是 <code>not null</code></p>
<h4 id="隐式等待">隐式等待<a class="header-anchor" href="#隐式等待"> ¶ </a></h4>
<p>当我们要找一个或者一些不能立即可用的元素的时候，隐式<code>waits</code>会告诉WebDriver轮询DOM指定的次数，默认设置是0次。一旦设定，WebDriver对象实例的整个生命周期的隐式调用也就设定好了。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium <span class="keyword">import</span> webdriver</span><br><span class="line"></span><br><span class="line">driver = webdriver.Firefox()</span><br><span class="line">driver.implicitly_wait(<span class="number">10</span>)		<span class="comment"># 设置隐式等待 最大等待时间</span></span><br><span class="line">driver.get(<span class="string">&quot;http://somedomain/url_that_delays_loading&quot;</span>)</span><br><span class="line">myDynamicElement = driver.find_element_by_id(<span class="string">&#x27;myDynamicElement&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h3 id="行为链">行为链<a class="header-anchor" href="#行为链"> ¶ </a></h3>
<p>更多函数查阅<a target="_blank" rel="noopener" href="https://selenium-python.readthedocs.io/api.html#module-selenium.webdriver.common.action_chains">7. WebDriver API — Selenium Python Bindings 2 documentation (selenium-python.readthedocs.io)</a></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> selenium.webdriver <span class="keyword">import</span> ActionChains	<span class="comment">#导入类</span></span><br><span class="line">action = ActionChains(bro)	<span class="comment">#初始化类，bro为浏览器对象</span></span><br><span class="line">action.click_and_hold(div)	<span class="comment">#点击并拖住，div为查找的一个元素</span></span><br><span class="line">action.move_by_offset(x,y).perform()	<span class="comment">#移动x,y像素，perform立即执行</span></span><br><span class="line"></span><br><span class="line">action.release()	<span class="comment"># 释放动作链</span></span><br></pre></td></tr></table></figure>
<h3 id="frame切换-窗口切换">frame切换/窗口切换<a class="header-anchor" href="#frame切换-窗口切换"> ¶ </a></h3>
<ul>
<li>
<p>需要定位iframe标签内部标签（子页面）</p>
<ul>
<li>
<p>需要先切换到子页面</p>
</li>
<li>
<pre><code class="language-python">bro.switch_to.frame([id])	#先切换作用域再定位
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 切换回主页面</span><br><span class="line"></span><br><span class="line">  - ```python</span><br><span class="line">    wd.switch_to.default_content()</span><br></pre></td></tr></table></figure>

</code></pre>
</li>
</ul>
</li>
<li>
<p>切换到新窗口</p>
<ul>
<li>
<pre><code class="language-python">for handle in wd.window_handles:
    # 先切换到该窗口
    wd.switch_to.window(handle)
    # 得到该窗口的标题栏字符串，判断是不是我们要操作的那个窗口
    if '窗口标题字符串' in wd.title:
        # 如果是，那么这时候WebDriver对象就是对应的该该窗口，正好，跳出循环，
        break
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- 切换回旧窗口：先保存旧窗口句柄，然后再切换</span><br><span class="line"></span><br><span class="line">  - ```python</span><br><span class="line">    # mainWindow变量保存当前窗口的句柄</span><br><span class="line">    mainWindow = wd.current_window_handle</span><br><span class="line">    #通过前面保存的老窗口的句柄，自己切换到老窗口</span><br><span class="line">    wd.switch_to.window(mainWindow)</span><br></pre></td></tr></table></figure>

</code></pre>
</li>
</ul>
</li>
</ul>
<h3 id="选择框">选择框<a class="header-anchor" href="#选择框"> ¶ </a></h3>
<h4 id="radio-单选框">radio 单选框<a class="header-anchor" href="#radio-单选框"> ¶ </a></h4>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;s_radio&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;radio&quot;</span> <span class="attr">name</span>=<span class="string">&quot;teacher&quot;</span> <span class="attr">value</span>=<span class="string">&quot;小江老师&quot;</span>&gt;</span>小江老师<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;radio&quot;</span> <span class="attr">name</span>=<span class="string">&quot;teacher&quot;</span> <span class="attr">value</span>=<span class="string">&quot;小雷老师&quot;</span>&gt;</span>小雷老师<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;radio&quot;</span> <span class="attr">name</span>=<span class="string">&quot;teacher&quot;</span> <span class="attr">value</span>=<span class="string">&quot;小凯老师&quot;</span> <span class="attr">checked</span>=<span class="string">&quot;checked&quot;</span>&gt;</span>小凯老师</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 获取当前选中的元素</span></span><br><span class="line">element = wd.find_element_by_css_selector(</span><br><span class="line">  <span class="string">&#x27;#s_radio input[checked=checked]&#x27;</span>)</span><br><span class="line"><span class="built_in">print</span>(<span class="string">&#x27;当前选中的是: &#x27;</span> + element.get_attribute(<span class="string">&#x27;value&#x27;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 点选 小雷老师</span></span><br><span class="line">wd.find_element_by_css_selector(</span><br><span class="line">  <span class="string">&#x27;#s_radio input[value=&quot;小雷老师&quot;]&#x27;</span>).click()</span><br></pre></td></tr></table></figure>
<h4 id="checkbox-复选框">checkbox 复选框<a class="header-anchor" href="#checkbox-复选框"> ¶ </a></h4>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;s_checkbox&quot;</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">name</span>=<span class="string">&quot;teacher&quot;</span> <span class="attr">value</span>=<span class="string">&quot;小江老师&quot;</span>&gt;</span>小江老师<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">name</span>=<span class="string">&quot;teacher&quot;</span> <span class="attr">value</span>=<span class="string">&quot;小雷老师&quot;</span>&gt;</span>小雷老师<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line">  <span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">&quot;checkbox&quot;</span> <span class="attr">name</span>=<span class="string">&quot;teacher&quot;</span> <span class="attr">value</span>=<span class="string">&quot;小凯老师&quot;</span> <span class="attr">checked</span>=<span class="string">&quot;checked&quot;</span>&gt;</span>小凯老师</span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 先把 已经选中的选项全部点击一下</span></span><br><span class="line">elements = wd.find_elements_by_css_selector(</span><br><span class="line">  <span class="string">&#x27;#s_checkbox input[checked=&quot;checked&quot;]&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> element <span class="keyword">in</span> elements:</span><br><span class="line">    element.click()</span><br><span class="line"></span><br><span class="line"><span class="comment"># 再点击 小雷老师</span></span><br><span class="line">wd.find_element_by_css_selector(</span><br><span class="line">  <span class="string">&quot;#s_checkbox input[value=&#x27;小雷老师&#x27;]&quot;</span>).click()</span><br></pre></td></tr></table></figure>
<h4 id="select">select<a class="header-anchor" href="#select"> ¶ </a></h4>
<ul>
<li>
<p><code>select_by_value</code>：根据选项的 <strong>value属性值</strong>，选择元素。</p>
<ul>
<li>
<pre><code class="language-python"># &lt;option value=&quot;foo&quot;&gt;Bar&lt;/option&gt;

s.select_by_value('foo')
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">- `select_by_index`：根据选项的 **次序**（从0开始），选择元素</span><br><span class="line"></span><br><span class="line">- `select_by_visible_text`：根据选项的 **可见文本**，选择元素</span><br><span class="line"></span><br><span class="line">  - ```python</span><br><span class="line">    # &lt;option value=&quot;foo&quot;&gt;Bar&lt;/option&gt;</span><br><span class="line">    </span><br><span class="line">    s.select_by_visible_text(&#x27;Bar&#x27;)</span><br></pre></td></tr></table></figure>

</code></pre>
</li>
</ul>
</li>
<li>
<p><code>deselect_by_value</code> ：根据选项的value属性值， <strong>去除</strong>选中元素</p>
</li>
<li>
<p><code>deselect_by_index</code> ：根据选项的次序，去除选中元素</p>
</li>
<li>
<p><code>deselect_by_visible_text</code> ：根据选项的可见文本，去除选中元素</p>
</li>
<li>
<p><code>deselect_all</code> ：去除选中所有元素</p>
</li>
</ul>
<h5 id="select-单选下拉框">select 单选下拉框<a class="header-anchor" href="#select-单选下拉框"> ¶ </a></h5>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 导入Select类</span></span><br><span class="line"><span class="keyword">from</span> selenium.webdriver.support.ui <span class="keyword">import</span> Select</span><br><span class="line"></span><br><span class="line"><span class="comment"># 创建Select对象</span></span><br><span class="line">select = Select(wd.find_element_by_id(<span class="string">&quot;ss_single&quot;</span>))</span><br><span class="line"></span><br><span class="line"><span class="comment"># 通过 Select 对象选中小雷老师</span></span><br><span class="line">select.select_by_visible_text(<span class="string">&quot;小雷老师&quot;</span>)</span><br></pre></td></tr></table></figure>
<h5 id="select-多选框">select 多选框<a class="header-anchor" href="#select-多选框"> ¶ </a></h5>
<p>多选框同</p>
<h2 id="图形界面">图形界面<a class="header-anchor" href="#图形界面"> ¶ </a></h2>
<h3 id="pyside2">pyside2<a class="header-anchor" href="#pyside2"> ¶ </a></h3>
<h4 id="安装-3">安装<a class="header-anchor" href="#安装-3"> ¶ </a></h4>
<figure class="highlight cmd"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">python -m pip install pyside2</span><br></pre></td></tr></table></figure>
<h4 id="控件">控件<a class="header-anchor" href="#控件"> ¶ </a></h4>
<h5 id="按钮">按钮<a class="header-anchor" href="#按钮"> ¶ </a></h5>
<p><code>QPushButton</code> 就是常见的按钮</p>
<h6 id="信号：被点击">信号：被点击<a class="header-anchor" href="#信号：被点击"> ¶ </a></h6>
<p>当按钮被点击就会发出 <code>clicked</code> 信号，可以这样指定处理该信号的函数</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">button.clicked.connect(handleCalc)</span><br></pre></td></tr></table></figure>
<h6 id="方法：改变文本">方法：改变文本<a class="header-anchor" href="#方法：改变文本"> ¶ </a></h6>
<p>代码中可以使用 <code>setText</code> 方法来改变按钮文本，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">button.setText(text)</span><br></pre></td></tr></table></figure>
<h6 id="方法：禁用、启用">方法：禁用、启用<a class="header-anchor" href="#方法：禁用、启用"> ¶ </a></h6>
<p>所有控件（继承自QWidget类）都支持 禁用和启用方法。</p>
<p>禁用后，该控件不再处理用户操作</p>
<ul>
<li>禁用</li>
</ul>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">button.setEnabled(<span class="literal">False</span>)</span><br></pre></td></tr></table></figure>
<ul>
<li>启用</li>
</ul>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">button.setEnabled(<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>
<h5 id="单行文本框">单行文本框<a class="header-anchor" href="#单行文本框"> ¶ </a></h5>
<p><code>QLineEdit</code> 是只能单行编辑的文本框。</p>
<h6 id="信号：文本被修改">信号：文本被修改<a class="header-anchor" href="#信号：文本被修改"> ¶ </a></h6>
<p>当文本框中的内容被键盘编辑，被点击就会发出 <code>textChanged </code>信号，可以这样指定处理该信号的函数</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.textChanged.connect(handleTextChange)</span><br></pre></td></tr></table></figure>
<h6 id="信号：按下回车键">信号：按下回车键<a class="header-anchor" href="#信号：按下回车键"> ¶ </a></h6>
<p>当用户在文本框中任何时候按下回车键，就会发出 <code>returnPressed</code> 信号。</p>
<p>有时我们需要处理这种情况，比如登录界面，用户输完密码直接按回车键就进行登录处理，比再用鼠标点击登录按钮快捷的多。</p>
<p>可以指定处理 returnPressed 信号，如下所示</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">passwordEdit.returnPressed.connect(onLogin)</span><br></pre></td></tr></table></figure>
<h6 id="方法：获取文本">方法：获取文本<a class="header-anchor" href="#方法：获取文本"> ¶ </a></h6>
<p>通过 <code>text</code> 方法获取编辑框内的文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">text = edit.text()</span><br></pre></td></tr></table></figure>
<h6 id="方法：设置提示">方法：设置提示<a class="header-anchor" href="#方法：设置提示"> ¶ </a></h6>
<p>通过 <code>setPlaceholderText</code> 方法可以设置提示文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.setPlaceholderText(<span class="string">&#x27;请在这里输入URL&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h6 id="方法：设置文本">方法：设置文本<a class="header-anchor" href="#方法：设置文本"> ¶ </a></h6>
<p>通过 <code>setText</code> 方法设置编辑框内的文本内容为参数里面的文本字符串，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.setText(<span class="string">&#x27;你好，白月黑羽&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>原来的所有内容会被清除</p>
<h6 id="方法：清除所有文本">方法：清除所有文本<a class="header-anchor" href="#方法：清除所有文本"> ¶ </a></h6>
<p><code>clear</code> 方法可以清除编辑框内所有的文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.clear()</span><br></pre></td></tr></table></figure>
<h6 id="方法：拷贝文本到剪贴板">方法：拷贝文本到剪贴板<a class="header-anchor" href="#方法：拷贝文本到剪贴板"> ¶ </a></h6>
<p><code>copy</code> 方法可以拷贝当前选中文本到剪贴板，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.copy()</span><br></pre></td></tr></table></figure>
<h6 id="方法：粘贴剪贴板文本">方法：粘贴剪贴板文本<a class="header-anchor" href="#方法：粘贴剪贴板文本"> ¶ </a></h6>
<p><code>paste</code> 方法可以把剪贴板内容，拷贝到编辑框当前光标所在处，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.paste()</span><br></pre></td></tr></table></figure>
<h5 id="多行纯文本框">多行纯文本框<a class="header-anchor" href="#多行纯文本框"> ¶ </a></h5>
<p><code>QPlainTextEdit</code> 是可以 <code>多行</code> 的纯文本编辑框。</p>
<h6 id="信号：文本被修改-2">信号：文本被修改<a class="header-anchor" href="#信号：文本被修改-2"> ¶ </a></h6>
<p>当文本框中的内容被键盘编辑，被点击就会发出 <code>textChanged </code>信号，可以这样指定处理该信号的函数</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.textChanged.connect(handleTextChange)</span><br></pre></td></tr></table></figure>
<h6 id="信号：光标位置改变">信号：光标位置改变<a class="header-anchor" href="#信号：光标位置改变"> ¶ </a></h6>
<p>当文本框中的光标位置变动，就会发出 <code>cursorPositionChanged</code> 信号，可以这样指定处理该信号的函数</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.cursorPositionChanged.connect(handleChanged)</span><br></pre></td></tr></table></figure>
<h6 id="方法：获取文本-2">方法：获取文本<a class="header-anchor" href="#方法：获取文本-2"> ¶ </a></h6>
<p>通过 <code>toPlainText</code> 方法获取编辑框内的文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">text = edit.toPlainText()</span><br></pre></td></tr></table></figure>
<h6 id="获取选中文本">获取选中文本<a class="header-anchor" href="#获取选中文本"> ¶ </a></h6>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#### 获取 QTextCursor 对象</span></span><br><span class="line">textCursor = edit.textCursor()</span><br><span class="line">selection = textCursor.selectedText()</span><br></pre></td></tr></table></figure>
<h6 id="方法：设置提示-2">方法：设置提示<a class="header-anchor" href="#方法：设置提示-2"> ¶ </a></h6>
<p>通过 <code>setPlaceholderText</code> 方法可以设置提示文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.setPlaceholderText(<span class="string">&#x27;请在这里输入薪资表&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h6 id="方法：设置文本-2">方法：设置文本<a class="header-anchor" href="#方法：设置文本-2"> ¶ </a></h6>
<p>通过 <code>setPlainText</code> 方法设置编辑框内的文本内容 为参数里面的文本字符串，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">edit.setPlainText(<span class="string">&#x27;&#x27;&#x27;你好，白月黑羽</span></span><br><span class="line"><span class="string">hello byhy&#x27;&#x27;&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>原来的所有内容会被清除</p>
<h6 id="方法：在末尾添加文本">方法：在末尾添加文本<a class="header-anchor" href="#方法：在末尾添加文本"> ¶ </a></h6>
<p>通过 <code>appendPlainText</code> 方法在编辑框末尾添加文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.appendPlainText(<span class="string">&#x27;你好，白月黑羽&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>注意：这种方法会在添加文本后 <code>自动换行</code></p>
<h6 id="方法：在光标处插入文本">方法：在光标处插入文本<a class="header-anchor" href="#方法：在光标处插入文本"> ¶ </a></h6>
<p>通过 <code>insertPlainText</code> 方法在编辑框末尾添加文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.insertPlainText(<span class="string">&#x27;你好，白月黑羽&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>注意：这种方法 <code>不会</code> 在添加文本后自动换行</p>
<h6 id="方法：清除所有文本-2">方法：清除所有文本<a class="header-anchor" href="#方法：清除所有文本-2"> ¶ </a></h6>
<p><code>clear</code> 方法可以清除编辑框内所有的文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.clear()</span><br></pre></td></tr></table></figure>
<h6 id="方法：拷贝文本到剪贴板-2">方法：拷贝文本到剪贴板<a class="header-anchor" href="#方法：拷贝文本到剪贴板-2"> ¶ </a></h6>
<p><code>copy</code> 方法可以清除编辑框内所有的文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.copy()</span><br></pre></td></tr></table></figure>
<h6 id="方法：粘贴剪贴板文本-2">方法：粘贴剪贴板文本<a class="header-anchor" href="#方法：粘贴剪贴板文本-2"> ¶ </a></h6>
<p><code>paste</code> 方法可以把剪贴板内容，拷贝到编辑框当前光标所在处，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.paste()</span><br></pre></td></tr></table></figure>
<h5 id="文本浏览框">文本浏览框<a class="header-anchor" href="#文本浏览框"> ¶ </a></h5>
<p><code>QTextBrowser</code> 是 <code>只能查看文本</code> 控件。</p>
<p>通常用来显示一些操作日志信息、或者不需要用户编辑的大段文本内容。</p>
<p><a target="_blank" rel="noopener" href="https://doc.qt.io/qtforpython-5.12/PySide2/QtWidgets/QTextBrowser.html">官网介绍</a></p>
<p>该控件 获取文本、设置文本、清除文本、剪贴板复制粘贴 等等， 都和上面介绍的 多行纯文本框是一样的。</p>
<p>下面我们主要讲解不同点</p>
<h6 id="方法：在末尾添加文本-2">方法：在末尾添加文本<a class="header-anchor" href="#方法：在末尾添加文本-2"> ¶ </a></h6>
<p>通过 <code>append</code> 方法在编辑框末尾添加文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">textBrowser.append(<span class="string">&#x27;你好，白月黑羽&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>有时，浏览框里面的内容长度超出了可见范围，我们在末尾添加了内容，往往希望控件自动翻滚到当前添加的这行，</p>
<p>可以通过 <code>ensureCursorVisible</code> 方法来实现</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">textBrowser.append(<span class="string">&#x27;你好，白月黑羽&#x27;</span>)</span><br><span class="line">textBrowser.ensureCursorVisible()</span><br></pre></td></tr></table></figure>
<p>注意：这种方法会在添加文本后 <code>自动换行</code></p>
<h6 id="方法：在光标处插入文本-2">方法：在光标处插入文本<a class="header-anchor" href="#方法：在光标处插入文本-2"> ¶ </a></h6>
<p>通过 <code>insertPlainText</code> 方法在编辑框末尾添加文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">edit.insertPlainText(<span class="string">&#x27;你好，白月黑羽&#x27;</span>)</span><br></pre></td></tr></table></figure>
<p>注意：这种方法 <code>不会</code> 在添加文本后自动换行</p>
<h5 id="文本标签">文本标签<a class="header-anchor" href="#文本标签"> ¶ </a></h5>
<p><code>QLabel</code> 就是常见的标签</p>
<h6 id="方法：改变文本-2">方法：改变文本<a class="header-anchor" href="#方法：改变文本-2"> ¶ </a></h6>
<p>代码中可以使用 <code>setText</code> 方法来改变标签文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">button.setText(text)</span><br></pre></td></tr></table></figure>
<h6 id="显示图片">显示图片<a class="header-anchor" href="#显示图片"> ¶ </a></h6>
<p>QLabel可以用来显示图片，有时一个图片可以让界面好看很多</p>
<p>怎么用QLabel 显示图片呢？</p>
<p>可以在 Qt Designer上 属性编辑器 QLabel 栏 的 pixmap 属性设置中选择图片文件指定。</p>
<h5 id="组合选择框">组合选择框<a class="header-anchor" href="#组合选择框"> ¶ </a></h5>
<p><code>QComboBox</code> 是组合选择框</p>
<h6 id="信号：选项改变">信号：选项改变<a class="header-anchor" href="#信号：选项改变"> ¶ </a></h6>
<p>如果用户操作修改了QComboBox中的选项就会发出 <code>currentIndexChanged </code>信号，可以这样指定处理该信号的函数</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cbox.currentIndexChanged.connect(handleSelectionChange)</span><br></pre></td></tr></table></figure>
<h6 id="方法：添加一个选项">方法：添加一个选项<a class="header-anchor" href="#方法：添加一个选项"> ¶ </a></h6>
<p>代码中可以使用 <code>addItem</code> 方法来添加一个选项到 <code>末尾</code> ，参数就是选项文本</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cbox.addItem(<span class="string">&#x27;byhy&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h6 id="方法：添加多个选项">方法：添加多个选项<a class="header-anchor" href="#方法：添加多个选项"> ¶ </a></h6>
<p>代码中可以使用 <code>addItems</code> 方法来添加多个选项到 <code>末尾</code>，参数是包含了多个选项文本的列表</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cbox.addItems([<span class="string">&#x27;byhy&#x27;</span>,<span class="string">&#x27;白月黑羽&#x27;</span>,<span class="string">&#x27;python教程&#x27;</span>])</span><br></pre></td></tr></table></figure>
<h6 id="方法：清空选项">方法：清空选项<a class="header-anchor" href="#方法：清空选项"> ¶ </a></h6>
<p>代码中可以使用 <code>clear</code> 方法来清空选项，也就是删除选择框内所有的选项</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">cbox.clear()</span><br></pre></td></tr></table></figure>
<h6 id="方法：获取当前选项文本">方法：获取当前选项文本<a class="header-anchor" href="#方法：获取当前选项文本"> ¶ </a></h6>
<p>代码中可以使用 <code>currentText</code> 方法来获取当前 <code>选中的选项</code> 的文本，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">method = cbox.currentText()</span><br></pre></td></tr></table></figure>
<h5 id="列表-2">列表<a class="header-anchor" href="#列表-2"> ¶ </a></h5>
<p><code>QListWidget</code> 是列表控件</p>
<h6 id="方法：添加一个选项-2">方法：添加一个选项<a class="header-anchor" href="#方法：添加一个选项-2"> ¶ </a></h6>
<p>代码中可以使用 <code>addItem</code> 方法来添加一个选项到 <code>末尾</code> ，参数就是选项文本</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">listWidget.addItem(<span class="string">&#x27;byhy&#x27;</span>)</span><br></pre></td></tr></table></figure>
<h6 id="方法：添加多个选项-2">方法：添加多个选项<a class="header-anchor" href="#方法：添加多个选项-2"> ¶ </a></h6>
<p>代码中可以使用 <code>addItems</code> 方法来添加多个选项到 <code>末尾</code>，参数是包含了多个选项文本的列表</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">listWidget.addItems([<span class="string">&#x27;byhy&#x27;</span>,<span class="string">&#x27;白月黑羽&#x27;</span>,<span class="string">&#x27;python教程&#x27;</span>])</span><br></pre></td></tr></table></figure>
<h6 id="方法：删除一个选项">方法：删除一个选项<a class="header-anchor" href="#方法：删除一个选项"> ¶ </a></h6>
<p>代码中可以使用 <code>takeItem</code> 方法来删除1个选项，参数是该选项所在行</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">listWidget.takeItem(<span class="number">1</span>)</span><br></pre></td></tr></table></figure>
<p>就会删除第二行选项</p>
<h6 id="方法：清空选项-2">方法：清空选项<a class="header-anchor" href="#方法：清空选项-2"> ¶ </a></h6>
<p>代码中可以使用 <code>clear</code> 方法来清空选项，也就是删除选择框内所有的选项</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">listWidget.clear()</span><br></pre></td></tr></table></figure>
<h6 id="方法：获取当前选项文本-2">方法：获取当前选项文本<a class="header-anchor" href="#方法：获取当前选项文本-2"> ¶ </a></h6>
<p><code>currentItem</code> 方法可以得到列表当前选中项对象（QListWidgetItem） ，再调用这个对象的 <code>text</code> 方法，就可以获取文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">listWidget.currentItem().text()</span><br></pre></td></tr></table></figure>
<p>就获取了 <code>第1行，第1列</code> 的单元格里面的文本。</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">listWidget.currentItem().text()</span><br></pre></td></tr></table></figure>
<h5 id="表格">表格<a class="header-anchor" href="#表格"> ¶ </a></h5>
<p><code>QTableWidget</code> 是表格控件</p>
<h6 id="创建列-和-标题栏">创建列 和 标题栏<a class="header-anchor" href="#创建列-和-标题栏"> ¶ </a></h6>
<p>我们可以通过 Qt designer 为一个表格创建列和对应的标题栏。</p>
<p>只需要双击 Qt designer 设计的窗体中的 表格控件， 就会出现这样的对话框。</p>
<p>在 <code>列</code> 标签栏中，点击左下角的加号，就可以为 添加一个列，并且设置标题栏名称。</p>
<h6 id="方法：设置表格行数">方法：设置表格行数<a class="header-anchor" href="#方法：设置表格行数"> ¶ </a></h6>
<p>代码中可以使用 <code>setRowCount</code> 方法来设置表格行数，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">table.setRowCount(<span class="number">10</span>)</span><br></pre></td></tr></table></figure>
<h6 id="方法：获取所有行数">方法：获取所有行数<a class="header-anchor" href="#方法：获取所有行数"> ¶ </a></h6>
<p>代码中可以使用 <code>rowCount</code> 方法来获取表格所有的函数，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">rowcount = table.rowCount()</span><br></pre></td></tr></table></figure>
<h6 id="方法：获取当前选中是第几行">方法：获取当前选中是第几行<a class="header-anchor" href="#方法：获取当前选中是第几行"> ¶ </a></h6>
<p>代码中可以使用 <code>currentRow</code> 方法来获取当前选中是第几行，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">currentrow = table.currentRow()</span><br></pre></td></tr></table></figure>
<p>注意：行数是从0开始的， 第一行的行数是 0</p>
<h6 id="方法：插入一行、删除一行">方法：插入一行、删除一行<a class="header-anchor" href="#方法：插入一行、删除一行"> ¶ </a></h6>
<p><code>insertRow</code> 方法可以在指定位置插入一行，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">table.insertRow(<span class="number">0</span>)</span><br></pre></td></tr></table></figure>
<p>就插入一行到第 <code>1</code> 行这个位置， 表格原来第1行（包括原来的第1行）以后的内容，全部往下移动一行。</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">table.insertRow(<span class="number">2</span>)</span><br></pre></td></tr></table></figure>
<p>就插入一行到第 <code>3</code> 行这个位置， 表格原来第3行（包括原来的第3行）以后的内容，全部往下移动一行。</p>
<p><code>removeRow</code> 方法可以删除指定位置的一行，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">table.removeRow(<span class="number">0</span>)</span><br></pre></td></tr></table></figure>
<p>就删除第 <code>1</code> 行， 表格原来第1行以后的内容，全部往上移动一行。</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">table.removeRow(<span class="number">2</span>)</span><br></pre></td></tr></table></figure>
<p>就删除第 <code>3</code> 行， 表格原来第3行以后的内容，全部往上移动一行。</p>
<h6 id="方法：清除-删除所有内容">方法：清除/删除所有内容<a class="header-anchor" href="#方法：清除-删除所有内容"> ¶ </a></h6>
<p><code>clearContents</code> 方法可以清除表格所有的内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">table.clearContents()</span><br></pre></td></tr></table></figure>
<p>清除后，仍然会留下表格栏</p>
<p>如果连表格栏都要删除，可以加上 <code>setRowCount(0)</code>，像这样</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">table.clearContents()</span><br><span class="line">table.setRowCount(<span class="number">0</span>)</span><br></pre></td></tr></table></figure>
<h6 id="方法：获取单元格文本的内容">方法：获取单元格文本的内容<a class="header-anchor" href="#方法：获取单元格文本的内容"> ¶ </a></h6>
<p><code>item</code> 方法可以指定位置的单元格对象（QTableWidgetItem） ，再调用这个对象的 <code>text</code> 方法，就可以获取文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">table.item(<span class="number">0</span>,<span class="number">0</span>).text()</span><br></pre></td></tr></table></figure>
<p>就获取了 <code>第1行，第1列</code> 的单元格里面的文本。</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">table.item(<span class="number">2</span>,<span class="number">4</span>).text()</span><br></pre></td></tr></table></figure>
<p>就获取了 <code>第3行，第5列</code> 的单元格里面的文本。</p>
<h6 id="方法：设置单元格文本内容">方法：设置单元格文本内容<a class="header-anchor" href="#方法：设置单元格文本内容"> ¶ </a></h6>
<p>qt表格的单元格内的内容对象 是一个 单元格对象 <code>QTableWidgetItem</code> 实例</p>
<p>如果单元格 <code>没有被设置过</code> 内容，可以这样</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PySide2.QtWidgets <span class="keyword">import</span> QTableWidgetItem</span><br><span class="line"></span><br><span class="line">item = QTableWidgetItem()</span><br><span class="line">item.setText(<span class="string">&#x27;白月黑羽&#x27;</span>)</span><br><span class="line">table.setItem(row, <span class="number">0</span>, item)</span><br></pre></td></tr></table></figure>
<p>也可以简写为</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PySide2.QtWidgets <span class="keyword">import</span> QTableWidgetItem</span><br><span class="line"></span><br><span class="line">table.setItem(row, <span class="number">0</span>, QTableWidgetItem(<span class="string">&#x27;白月黑羽&#x27;</span>))</span><br></pre></td></tr></table></figure>
<p>如果单元格 <code>已经被设置过</code> 文本内容，</p>
<p><code>item</code> 方法可以获取指定位置的 QTableWidgetItem ，再调用这个对象的 <code>setText</code> 方法，就可以设置单元格文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">table.item(<span class="number">0</span>,<span class="number">0</span>).setText()</span><br></pre></td></tr></table></figure>
<p>就设置了 <code>第1行，第1列</code> 的单元格里面的文本。</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">table.item(<span class="number">2</span>,<span class="number">4</span>).setText()</span><br></pre></td></tr></table></figure>
<p>就设置了 <code>第3行，第5列</code> 的单元格里面的文本。</p>
<p>如果希望某个单元格为 <strong>只读</strong>，不允许修改，可以使用QTableWidgetItem对象的 <code>setFlags</code> 方法，像这样</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PySide2.QtWidgets <span class="keyword">import</span> QTableWidgetItem</span><br><span class="line"><span class="keyword">from</span> PySide2.QtCore <span class="keyword">import</span> Qt</span><br><span class="line"></span><br><span class="line">item = QTableWidgetItem(<span class="string">&#x27;白月黑羽&#x27;</span>)</span><br><span class="line">item.setFlags(Qt.ItemIsEnabled) <span class="comment">#### 参数名字段不允许修改</span></span><br><span class="line">table.setItem(row, <span class="number">0</span>, item)</span><br></pre></td></tr></table></figure>
<p>如果想文本内容 <strong>居中对齐</strong>，每个当对应的QTableWidgetItem 调用 setTextAlignment，如下</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PySide2.QtWidgets <span class="keyword">import</span> QTableWidgetItem</span><br><span class="line"><span class="keyword">from</span> PySide2.QtCore <span class="keyword">import</span> Qt</span><br><span class="line"></span><br><span class="line">item = QTableWidgetItem()</span><br><span class="line">item.setText(<span class="string">&#x27;白月黑羽&#x27;</span>)</span><br><span class="line"><span class="comment">#### 文本居中</span></span><br><span class="line">item.setTextAlignment(Qt.AlignHCenter) </span><br><span class="line">table.setItem(row, <span class="number">0</span>, item)</span><br></pre></td></tr></table></figure>
<h6 id="方法：设定列宽、宽度自动缩放">方法：设定列宽、宽度自动缩放<a class="header-anchor" href="#方法：设定列宽、宽度自动缩放"> ¶ </a></h6>
<p>Qt Designer 上目前没法拖拽设定 每个列的宽度，只能在代码中指定。</p>
<p>如下所示</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#### 设定第1列的宽度为 180像素</span></span><br><span class="line">table.setColumnWidth(<span class="number">0</span>, <span class="number">180</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#### 设定第2列的宽度为 100像素</span></span><br><span class="line">table.setColumnWidth(<span class="number">1</span>, <span class="number">100</span>)</span><br></pre></td></tr></table></figure>
<p>如想让 表格控件宽度 随着父窗口的缩放自动缩放，可以</p>
<p>在 属性编辑器 中 勾选 <code>HorizontalHeaderStretchLastSection</code></p>
<p>或者使用下面代码</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">table.horizontalHeader().setStretchLastSection(<span class="literal">True</span>)</span><br></pre></td></tr></table></figure>
<h6 id="信号：单元格内容改动">信号：单元格内容改动<a class="header-anchor" href="#信号：单元格内容改动"> ¶ </a></h6>
<p>当用户修改了一个单元格的内容，会发出 <code>cellChanged</code> 信号，并且携带参数指明该单元格的行号和列号。</p>
<p>我们的代码可以对该信号进行相应的处理。</p>
<p>示例代码如下</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">    <span class="comment">#### 指定单元格改动信号处理函数</span></span><br><span class="line">    self.ui.table.cellChanged.connect(self.cfgItemChanged)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">cfgItemChanged</span>(<span class="params">self,row,column</span>):</span></span><br><span class="line">    <span class="comment">#### 获取更改内容</span></span><br><span class="line">    cfgName = self.ui.table.item(row, <span class="number">0</span>).text() <span class="comment">#### 首列为配置名称</span></span><br><span class="line">    cfgValue = self.ui.table.item(row, column).text()</span><br></pre></td></tr></table></figure>
<h5 id="单选按钮-和-按钮组">单选按钮 和 按钮组<a class="header-anchor" href="#单选按钮-和-按钮组"> ¶ </a></h5>
<p><code>QRadioButton</code> 是单选按钮</p>
<h6 id="说明">说明<a class="header-anchor" href="#说明"> ¶ </a></h6>
<p><code>同一个父窗口</code> 里面的多个单选按钮，只能选中一项。</p>
<p>如果你有多组单选按钮， 每组都应该有不同的父控件，或者不同的Layout。</p>
<p>通常建议：多组单选按钮，放到不同的 按钮组 <code>QButtonGroup</code> 中，按钮组就是父控件</p>
<h6 id="信号：选中状态改变">信号：选中状态改变<a class="header-anchor" href="#信号：选中状态改变"> ¶ </a></h6>
<p>如果用户操作点击了按钮组 <code>QButtonGroup</code> 中的一个按钮， QButtonGroup 就会发出 <code>buttonClicked</code> 信号，可以这样指定处理该信号的函数</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">buttongroup.buttonClicked.connect(handleButtonClicked)</span><br></pre></td></tr></table></figure>
<p>然后，在处理函数中调用QButtonGroup对象的 <code>checkedButton()</code> 函数，返回值就是被选中的按钮对象。</p>
<p>再调用这个返回的按钮对象的 <code>text()</code> 方法得到界面文本，就可以知道是哪个选项被选中了。</p>
<h5 id="勾选按钮-和-按钮组">勾选按钮 和 按钮组<a class="header-anchor" href="#勾选按钮-和-按钮组"> ¶ </a></h5>
<p><code>QCheckBox</code> 是勾选按钮</p>
<h6 id="说明-2">说明<a class="header-anchor" href="#说明-2"> ¶ </a></h6>
<p>通常建议：多组勾选按钮，放到不同的 按钮组 <code>QButtonGroup</code> 中，按钮组就是父控件。</p>
<h6 id="信号：选中状态改变-2">信号：选中状态改变<a class="header-anchor" href="#信号：选中状态改变-2"> ¶ </a></h6>
<p>如果用户操作点击了按钮组 <code>QButtonGroup</code> 中的一个按钮， QButtonGroup 就会发出 <code>buttonClicked</code> 信号，可以这样指定处理该信号的函数</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">buttongroup.buttonClicked.connect(handleButtonClicked)</span><br></pre></td></tr></table></figure>
<p>然后，在处理函数中调用QButtonGroup对象的 <code>checkedButton()</code> 函数，返回值就是被选中的按钮对象。</p>
<p>再调用这个返回的按钮对象的 <code>text()</code> 方法得到界面文本，就可以知道是哪个选项被选中了。</p>
<h5 id="tab页控件">tab页控件<a class="header-anchor" href="#tab页控件"> ¶ </a></h5>
<p>我们可以通过tab页控件把界面分为好几各页面</p>
<p>通过Qt designer 只需要拖拽控件到各个页面即可。</p>
<p>要修改tab页的标题，可以先点击该tab页，然后在属性编辑器<code>currentTabText</code>修改</p>
<h6 id="tab页中布局Layout">tab页中布局Layout<a class="header-anchor" href="#tab页中布局Layout"> ¶ </a></h6>
<p>如果我们要在tab页上布局， 你可能会在对象查看器总直接右键点击该tab，可以你会发现 右键菜单里面没有布局项。</p>
<p>这是 Qt designer 非常坑爹的地方，我当时足足花了一个小时才找到方法。</p>
<ol>
<li>首先需要你在tab页上添加一个控件</li>
<li>然后点击 在对象查看器 右键点击上层 TabWidget ，这时，你就会发现有布局菜单了</li>
</ol>
<h5 id="进度条">进度条<a class="header-anchor" href="#进度条"> ¶ </a></h5>
<p><code>QProgressBar</code> 是进度条，如下图所示</p>
<h6 id="说明-3">说明<a class="header-anchor" href="#说明-3"> ¶ </a></h6>
<p>进度条也是一个常用的控件，当程序需要做一件比较耗费时间的任务（比如统计数据，下载文件等）时，可以用来向用户指示操作的进度。</p>
<p>而且有了进度显示，用户就知道应用程序仍在运行，并没有出问题。</p>
<p>QProgressBar进度条把每个进度称之为一个step（步骤）。</p>
<p>我们可以通过它的 <code>setRange</code> 方法设定步骤个数，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">progressBar.setRange(<span class="number">0</span>,<span class="number">5</span>)</span><br></pre></td></tr></table></figure>
<p>就设定了，进度分为5步。</p>
<p>然后，通过 <code>setValue</code> 方法，指定当前完成到了哪一步，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">progressBar.setValue(<span class="number">3</span>)</span><br></pre></td></tr></table></figure>
<p>就表示完成了 3/5， 也就是 60%， 进度条就会显示60%的进度。</p>
<p>可以使用reset()将进度条倒退到开头。</p>
<p>有时候我们的任务没法知道完成了多少，比如下载一个未知大小的文件。</p>
<p>这时，可以把range 范围都设置为0，这样，进度条会显示忙碌指示符，而不是显示进度百分比。</p>
<p>下面是一个进度条程序的示例代码</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PySide2.QtWidgets <span class="keyword">import</span> QApplication, QMainWindow, QPushButton,  QProgressBar,QMessageBox</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span>  Thread</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Stats</span>():</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span></span><br><span class="line">        self.window = QMainWindow()</span><br><span class="line">        self.window.resize(<span class="number">500</span>, <span class="number">400</span>)</span><br><span class="line">        self.window.move(<span class="number">300</span>, <span class="number">300</span>)</span><br><span class="line"></span><br><span class="line">        self.progressBar = QProgressBar(self.window)</span><br><span class="line">        self.progressBar.resize(<span class="number">300</span>, <span class="number">20</span>)</span><br><span class="line">        self.progressBar.move(<span class="number">80</span>, <span class="number">30</span>)</span><br><span class="line">        <span class="comment">#### 进度是 0 - 5，</span></span><br><span class="line">        self.progressBar.setRange(<span class="number">0</span>,<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">        self.button = QPushButton(<span class="string">&#x27;统计&#x27;</span>, self.window)</span><br><span class="line">        self.button.move(<span class="number">80</span>, <span class="number">80</span>)</span><br><span class="line"></span><br><span class="line">        self.button.clicked.connect(self.handleCalc)</span><br><span class="line"></span><br><span class="line">        <span class="comment">#### 统计进行中标记，不能同时做两个统计</span></span><br><span class="line">        self.ongoing = <span class="literal">False</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">handleCalc</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">workerThreadFunc</span>():</span></span><br><span class="line">            self.ongoing = <span class="literal">True</span></span><br><span class="line">            <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>,<span class="number">6</span>):</span><br><span class="line">                sleep(<span class="number">1</span>)</span><br><span class="line">                <span class="comment">#### 设置进度值</span></span><br><span class="line">                self.progressBar.setValue(i)</span><br><span class="line">            self.ongoing = <span class="literal">False</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> self.ongoing:</span><br><span class="line">            QMessageBox.warning(</span><br><span class="line">                self.window,</span><br><span class="line">                <span class="string">&#x27;警告&#x27;</span>,<span class="string">&#x27;任务进行中，请等待完成&#x27;</span>)</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">#### 通常任务执行比较耗时，应该在新的线程中进行</span></span><br><span class="line">        <span class="comment">#### 否则会阻塞主线程显示界面</span></span><br><span class="line">        worker = Thread(target=workerThreadFunc)</span><br><span class="line">        worker.start()</span><br><span class="line"></span><br><span class="line">app = QApplication([])</span><br><span class="line">stats = Stats()</span><br><span class="line">stats.window.show()</span><br><span class="line">app.exec_()</span><br></pre></td></tr></table></figure>
<p>上面的代码，运行时，会有很多告警，因为我们在新线程中操作界面对象，容易出问题。</p>
<p>更合理的方法是通过信号，在线程之间传递信息，对界面的操作都在主线程中完成。</p>
<p>如下</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PySide2.QtWidgets <span class="keyword">import</span> QApplication, QMainWindow, QPushButton,  QProgressBar,QMessageBox</span><br><span class="line"><span class="keyword">from</span> time <span class="keyword">import</span> sleep</span><br><span class="line"><span class="keyword">from</span> threading <span class="keyword">import</span>  Thread</span><br><span class="line"><span class="keyword">from</span> PySide2.QtCore <span class="keyword">import</span> Signal,QObject</span><br><span class="line"></span><br><span class="line"><span class="comment">#### 信号库</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">SignalStore</span>(<span class="params">QObject</span>):</span></span><br><span class="line">    <span class="comment">#### 定义一种信号</span></span><br><span class="line">    progress_update = Signal(<span class="built_in">int</span>)</span><br><span class="line">    <span class="comment">#### 还可以定义其他作用的信号</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#### 实例化</span></span><br><span class="line">so = SignalStore()</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Stats</span>():</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self</span>):</span> </span><br><span class="line">        <span class="comment">#### 连接信号到处理的slot函数</span></span><br><span class="line">        so.progress_update.connect(self.setProgress)</span><br><span class="line">        </span><br><span class="line">        self.window = QMainWindow()</span><br><span class="line">        self.window.resize(<span class="number">500</span>, <span class="number">400</span>)</span><br><span class="line">        self.window.move(<span class="number">300</span>, <span class="number">300</span>)</span><br><span class="line"></span><br><span class="line">        self.progressBar = QProgressBar(self.window)</span><br><span class="line">        self.progressBar.resize(<span class="number">300</span>, <span class="number">20</span>)</span><br><span class="line">        self.progressBar.move(<span class="number">80</span>, <span class="number">30</span>)</span><br><span class="line">        <span class="comment">#### 进度是 0 - 5，</span></span><br><span class="line">        self.progressBar.setRange(<span class="number">0</span>,<span class="number">5</span>)</span><br><span class="line"></span><br><span class="line">        self.button = QPushButton(<span class="string">&#x27;统计&#x27;</span>, self.window)</span><br><span class="line">        self.button.move(<span class="number">80</span>, <span class="number">80</span>)</span><br><span class="line"></span><br><span class="line">        self.button.clicked.connect(self.handleCalc)</span><br><span class="line"></span><br><span class="line">        <span class="comment">#### 统计进行中标记，不能同时做两个统计</span></span><br><span class="line">        self.ongoing = <span class="literal">False</span></span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">handleCalc</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">workerThreadFunc</span>():</span></span><br><span class="line">            self.ongoing = <span class="literal">True</span></span><br><span class="line">            <span class="keyword">for</span> i <span class="keyword">in</span> <span class="built_in">range</span>(<span class="number">1</span>,<span class="number">6</span>):</span><br><span class="line">                sleep(<span class="number">1</span>)</span><br><span class="line">                <span class="comment">#### 发出信息，通知主线程进行进度处理</span></span><br><span class="line">                so.progress_update.emit(i)</span><br><span class="line">            self.ongoing = <span class="literal">False</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> self.ongoing:</span><br><span class="line">            QMessageBox.warning(</span><br><span class="line">                self.window,</span><br><span class="line">                <span class="string">&#x27;警告&#x27;</span>,<span class="string">&#x27;任务进行中，请等待完成&#x27;</span>)</span><br><span class="line">            <span class="keyword">return</span></span><br><span class="line"></span><br><span class="line">        worker = Thread(target=workerThreadFunc)</span><br><span class="line">        worker.start()</span><br><span class="line"></span><br><span class="line">    <span class="comment">#### 处理进度的slot函数</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">setProgress</span>(<span class="params">self,value</span>):</span></span><br><span class="line">        self.progressBar.setValue(value)</span><br><span class="line"></span><br><span class="line">app = QApplication([])</span><br><span class="line">stats = Stats()</span><br><span class="line">stats.window.show()</span><br><span class="line">app.exec_()</span><br></pre></td></tr></table></figure>
<h5 id="数字输入框">数字输入框<a class="header-anchor" href="#数字输入框"> ¶ </a></h5>
<p><code>QSpinBox</code> 是数字输入框，可以输入或使用上下箭头选择数字</p>
<h6 id="获取数字">获取数字<a class="header-anchor" href="#获取数字"> ¶ </a></h6>
<p>通过 <code>value</code> 方法获取编辑框内的文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">number = box.value()</span><br></pre></td></tr></table></figure>
<p>注意：返回的是整数对象，不是字符串</p>
<h6 id="方法：设置数字">方法：设置数字<a class="header-anchor" href="#方法：设置数字"> ¶ </a></h6>
<p>通过 <code>setValue</code> 方法可以设置提示文本内容，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">box.setValue(<span class="number">100</span>)</span><br></pre></td></tr></table></figure>
<h5 id="日期控件">日期控件<a class="header-anchor" href="#日期控件"> ¶ </a></h5>
<p><code>QDateEdit</code> 类可以用来选择日期时间</p>
<h6 id="获取日期">获取日期<a class="header-anchor" href="#获取日期"> ¶ </a></h6>
<p>当用户点击日期时间控件并且选取了 日期和时间，后来程序要获取这个控件里面选定的日期时间，可以使用date方法获取日期对象。</p>
<p>如下所示</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#### 返回 PySide2.QtCore.QDate 对象</span></span><br><span class="line">qdate = dateEdit.date()</span><br><span class="line"></span><br><span class="line"><span class="comment">#### 可以转化为 指定格式的字符串</span></span><br><span class="line">dateStr = qdate.toString(<span class="string">&#x27;yyyy-MM-dd&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment">#### 也可以获取年月日 对应的数字 ，比如日期是2020年5月2号</span></span><br><span class="line">year = qdate.year()   <span class="comment">#### 返回 2020</span></span><br><span class="line">month = qdate.month() <span class="comment">#### 返回 5</span></span><br><span class="line">day = qdate.day()     <span class="comment">#### 返回 2</span></span><br></pre></td></tr></table></figure>
<p>QDate 对象的具体说明<a target="_blank" rel="noopener" href="https://doc.qt.io/qtforpython-5.12/PySide2/QtCore/QDate.html">参考官方文档</a></p>
<h5 id="选择文件框">选择文件框<a class="header-anchor" href="#选择文件框"> ¶ </a></h5>
<p><code>QFileDialog</code> 类可以用来选择文件或者目录</p>
<h6 id="选择目录">选择目录<a class="header-anchor" href="#选择目录"> ¶ </a></h6>
<p>通过 <code>getExistingDirectory 静态方法</code> 选择目录。</p>
<p>该方法，第一个参数是父窗口对象，第二个参数是选择框显示的标题。</p>
<p>比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PySide2.QtWidgets <span class="keyword">import</span> QFileDialog</span><br><span class="line"></span><br><span class="line">filePath = QFileDialog.getExistingDirectory(self.ui, <span class="string">&quot;选择存储路径&quot;</span>)</span><br></pre></td></tr></table></figure>
<p>返回值即为选择的路径字符串。</p>
<p>如果用户点击了 选择框的 取消选择按钮，返回 空字符串。</p>
<h6 id="选择单个文件">选择单个文件<a class="header-anchor" href="#选择单个文件"> ¶ </a></h6>
<p>如果你想弹出文件选择框，选择一个 <code>已经存在</code> 的文件，可以使用 QFileDialog 静态方法 <code>getOpenFileName</code> ，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PySide2.QtWidgets <span class="keyword">import</span> QFileDialog</span><br><span class="line"></span><br><span class="line">filePath, _  = QFileDialog.getOpenFileName(</span><br><span class="line">            self.ui,             <span class="comment">#### 父窗口对象</span></span><br><span class="line">            <span class="string">&quot;选择你要上传的图片&quot;</span>, <span class="comment">#### 标题</span></span><br><span class="line">            <span class="string">r&quot;d:\\data&quot;</span>,        <span class="comment">#### 起始目录</span></span><br><span class="line">            <span class="string">&quot;图片类型 (*.png *.jpg *.bmp)&quot;</span> <span class="comment">#### 选择类型过滤项，过滤内容在括号中</span></span><br><span class="line">        )</span><br></pre></td></tr></table></figure>
<p>该方法返回值 是一个元组，第一个元素是选择的文件路径，第二个元素是文件类型，如果你只想获取文件路径即可，可以采用上面的代码写法。</p>
<p>如果用户点击了 选择框的 取消选择按钮，返回 空字符串。</p>
<p>如果你想弹出文件选择框，选择路径和文件名，来 <code>保存一个文件</code> ，可以使用 QFileDialog 静态方法 <code>getSaveFileName</code> ，比如</p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PySide2.QtWidgets <span class="keyword">import</span> QFileDialog</span><br><span class="line"></span><br><span class="line">filePath, _  = QFileDialog.getSaveFileName(</span><br><span class="line">            self.ui,             <span class="comment">#### 父窗口对象</span></span><br><span class="line">            <span class="string">&quot;保存文件&quot;</span>, <span class="comment">#### 标题</span></span><br><span class="line">            <span class="string">r&quot;d:\\data&quot;</span>,        <span class="comment">#### 起始目录</span></span><br><span class="line">            <span class="string">&quot;json类型 (*.json)&quot;</span> <span class="comment">#### 选择类型过滤项，过滤内容在括号中</span></span><br><span class="line">        )</span><br></pre></td></tr></table></figure>
<h6 id="选择多个文件">选择多个文件<a class="header-anchor" href="#选择多个文件"> ¶ </a></h6>
<p>如果要选择多个文件，使用 <code>getOpenFileNames 静态方法</code></p>
<figure class="highlight py"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> PySide2.QtWidgets <span class="keyword">import</span> QFileDialog</span><br><span class="line"></span><br><span class="line">filePaths, _  = QFileDialog.getOpenFileNames(</span><br><span class="line">            self.ui,             <span class="comment">#### 父窗口对象</span></span><br><span class="line">            <span class="string">&quot;选择你要上传的图片&quot;</span>, <span class="comment">#### 标题</span></span><br><span class="line">            <span class="string">r&quot;d:\\data&quot;</span>,        <span class="comment">#### 起始目录</span></span><br><span class="line">            <span class="string">&quot;图片类型 (*.png *.jpg *.bmp)&quot;</span> <span class="comment">#### 选择类型过滤项，过滤内容在括号中</span></span><br><span class="line">        )</span><br></pre></td></tr></table></figure>
<p>上例中 filePaths 对应的返回值是一个列表，里面包含了选择的文件。</p>
<p>如果用户点击了 选择框的 取消选择按钮，返回 空列表。</p>
<h2 id="库方法">库方法<a class="header-anchor" href="#库方法"> ¶ </a></h2>
<h2 id="random-库">random 库<a class="header-anchor" href="#random-库"> ¶ </a></h2>
<table>
<thead>
<tr>
<th style="text-align:center">函数/方法</th>
<th style="text-align:center">参数</th>
<th style="text-align:center">返回值/作用</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:center">random.choice()</td>
<td style="text-align:center">一个序列</td>
<td style="text-align:center">随机返回序列中的一个元素</td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
<tr>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
<td style="text-align:center"></td>
</tr>
</tbody>
</table>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Forgo7ten</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://forgo7ten.github.io/Development/python/Python_Development_Notes/">https://forgo7ten.github.io/Development/python/Python_Development_Notes/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://Forgo7ten.github.io" target="_blank">Forgo7ten'blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Python/">Python</a></div><div class="post_share"><div class="social-share" data-image="http://forgo7ten.gitee.io/store/imgs/12.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/images/wechat.png" target="_blank"><img class="post-qr-code-img" src="/images/wechat.png" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/images/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/images/alipay.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/Env/VSCode_self-use_plugin_record/"><img class="prev-cover" src="http://forgo7ten.gitee.io/store/imgs/8.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">【工具调教】VS Code 自用插件记录</div></div></a></div><div class="next-post pull-right"><a href="/Development/android/Android_Development_Notes/"><img class="next-cover" src="http://forgo7ten.gitee.io/store/imgs/10.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Android个人学习笔记</div></div></a></div></nav><hr/><div id="post-comment"><div class="comment-head"><div class="comment-headline"><i class="fas fa-comments fa-fw"></i><span> 评论</span></div></div><div class="comment-wrap"><div><div id="gitalk-container"></div></div></div></div></div><div class="aside-content" id="aside-content"><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Python%E5%AD%A6%E4%B9%A0"><span class="toc-number">1.</span> <span class="toc-text">Python学习</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.1.</span> <span class="toc-text">类型</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">1.1.1.</span> <span class="toc-text">类型转换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%A4%E6%96%AD%E7%B1%BB%E5%9E%8B"><span class="toc-number">1.1.2.</span> <span class="toc-text">判断类型</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-number">1.2.</span> <span class="toc-text">字符串</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-number">1.2.1.</span> <span class="toc-text">格式化</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#format%E6%A0%BC%E5%BC%8F%E5%8C%96"><span class="toc-number">1.2.1.1.</span> <span class="toc-text">format格式化</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A0%BC%E5%BC%8F%E5%8C%96%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="toc-number">1.2.1.2.</span> <span class="toc-text">%格式化操作符</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BA%8F%E5%88%97"><span class="toc-number">1.3.</span> <span class="toc-text">序列</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%88%97%E8%A1%A8"><span class="toc-number">1.3.1.</span> <span class="toc-text">列表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E5%88%97%E8%A1%A8"><span class="toc-number">1.3.2.</span> <span class="toc-text">生成列表</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E5%BA%8F%E5%88%97%E3%80%81%E5%85%83%E7%BB%84"><span class="toc-number">1.3.2.1.</span> <span class="toc-text">生成序列、元组</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%94%B6%E9%9B%86%E5%8F%82%E6%95%B0"><span class="toc-number">1.3.2.1.1.</span> <span class="toc-text">收集参数</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8range-%E5%88%9B%E5%BB%BA%E6%95%B0%E5%AD%97%E5%88%97%E8%A1%A8"><span class="toc-number">1.3.2.2.</span> <span class="toc-text">使用range()创建数字列表</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%97%E8%A1%A8%E7%9A%84%E5%88%87%E7%89%87"><span class="toc-number">1.3.2.3.</span> <span class="toc-text">列表的切片</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%97%E8%A1%A8%E7%9A%84%E5%A4%8D%E5%88%B6"><span class="toc-number">1.3.2.4.</span> <span class="toc-text">列表的复制</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%83%E7%BB%84"><span class="toc-number">1.3.3.</span> <span class="toc-text">元组</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9B%86%E5%90%88"><span class="toc-number">1.3.4.</span> <span class="toc-text">集合</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E9%9B%86%E5%90%88"><span class="toc-number">1.3.4.1.</span> <span class="toc-text">创建集合</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8D%E5%8F%AF%E5%8F%98%E9%9B%86%E5%90%88"><span class="toc-number">1.3.4.2.</span> <span class="toc-text">不可变集合</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#if"><span class="toc-number">1.4.</span> <span class="toc-text">if</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%B8%89%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">1.4.1.</span> <span class="toc-text">三元运算符</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BE%AA%E7%8E%AF"><span class="toc-number">1.5.</span> <span class="toc-text">循环</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#for%E5%BE%AA%E7%8E%AF"><span class="toc-number">1.5.1.</span> <span class="toc-text">for循环</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#while-%E5%BE%AA%E7%8E%AF"><span class="toc-number">1.5.2.</span> <span class="toc-text">while 循环</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E5%85%B8"><span class="toc-number">1.6.</span> <span class="toc-text">字典</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%81%8D%E5%8E%86%E5%AD%97%E5%85%B8"><span class="toc-number">1.6.1.</span> <span class="toc-text">遍历字典</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E7%9A%84%E9%94%AE%E5%80%BC%E5%AF%B9"><span class="toc-number">1.6.1.1.</span> <span class="toc-text">遍历所有的键值对</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E9%94%AE"><span class="toc-number">1.6.1.2.</span> <span class="toc-text">遍历所有键</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%81%8D%E5%8E%86%E6%89%80%E6%9C%89%E5%80%BC"><span class="toc-number">1.6.1.3.</span> <span class="toc-text">遍历所有值</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AD%97%E5%85%B8%E5%86%85%E7%BD%AE%E6%96%B9%E6%B3%95"><span class="toc-number">1.6.2.</span> <span class="toc-text">字典内置方法</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%87%BD%E6%95%B0"><span class="toc-number">1.7.</span> <span class="toc-text">函数</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#lambda%E5%87%BD%E6%95%B0"><span class="toc-number">1.7.1.</span> <span class="toc-text">lambda函数</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#filter-%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="toc-number">1.7.2.</span> <span class="toc-text">filter() 过滤器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#map"><span class="toc-number">1.7.3.</span> <span class="toc-text">map()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%96%87%E4%BB%B6%E7%B3%BB%E7%BB%9F"><span class="toc-number">1.8.</span> <span class="toc-text">文件系统</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%89%93%E5%BC%80%E6%96%87%E4%BB%B6"><span class="toc-number">1.8.1.</span> <span class="toc-text">打开文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#file-%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.8.2.</span> <span class="toc-text">file 对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#OS%E6%A8%A1%E5%9D%97"><span class="toc-number">1.8.3.</span> <span class="toc-text">OS模块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#pickle-%E4%BF%9D%E5%AD%98%E6%95%B0%E6%8D%AE"><span class="toc-number">1.8.4.</span> <span class="toc-text">pickle 保存数据</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%82%E5%B8%B8%E5%A4%84%E7%90%86"><span class="toc-number">1.9.</span> <span class="toc-text">异常处理</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8D%95%E8%8E%B7%E5%BC%82%E5%B8%B8"><span class="toc-number">1.9.1.</span> <span class="toc-text">捕获异常</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8"><span class="toc-number">1.9.2.</span> <span class="toc-text">抛出异常</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#else"><span class="toc-number">1.9.3.</span> <span class="toc-text">else</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#with%E8%AF%AD%E5%8F%A5"><span class="toc-number">1.9.4.</span> <span class="toc-text">with语句</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.10.</span> <span class="toc-text">类和对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AA%E7%B1%BB"><span class="toc-number">1.10.1.</span> <span class="toc-text">定义一个类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%85%AC%E6%9C%89%E3%80%81%E7%A7%81%E6%9C%89"><span class="toc-number">1.10.2.</span> <span class="toc-text">公有、私有</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E7%BB%A7%E6%89%BF"><span class="toc-number">1.10.3.</span> <span class="toc-text">类的继承</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E7%9A%84%E7%BB%84%E5%90%88"><span class="toc-number">1.10.4.</span> <span class="toc-text">类的组合</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E5%AF%B9%E8%B1%A1%E5%92%8C%E5%AE%9E%E4%BE%8B%E5%AF%B9%E8%B1%A1"><span class="toc-number">1.10.5.</span> <span class="toc-text">类对象和实例对象</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%BB%91%E5%AE%9A"><span class="toc-number">1.10.6.</span> <span class="toc-text">绑定</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%B1%BB%E5%92%8C%E5%AF%B9%E8%B1%A1-BIF-%E5%86%85%E7%BD%AE%E5%87%BD%E6%95%B0-%EF%BC%9A"><span class="toc-number">1.10.7.</span> <span class="toc-text">类和对象 BIF(内置函数)：</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%AD%94%E6%B3%95%E6%96%B9%E6%B3%95"><span class="toc-number">1.11.</span> <span class="toc-text">魔法方法</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F"><span class="toc-number">1.11.1.</span> <span class="toc-text">注意</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%9E%84%E9%80%A0%E5%92%8C%E6%9E%90%E6%9E%84"><span class="toc-number">1.11.2.</span> <span class="toc-text">构造和析构</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#init-self-%E2%80%A6"><span class="toc-number">1.11.2.1.</span> <span class="toc-text">__init__(self[,…])</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#new-cls-%E2%80%A6"><span class="toc-number">1.11.2.2.</span> <span class="toc-text">__new__(cls[, …])</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#del"><span class="toc-number">1.11.2.3.</span> <span class="toc-text">__del__</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%AE%97%E6%95%B0%E8%BF%90%E7%AE%97"><span class="toc-number">1.11.3.</span> <span class="toc-text">算数运算</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%B8%B8%E8%A7%81%E7%AE%97%E6%9C%AF%E8%BF%90%E7%AE%97"><span class="toc-number">1.11.3.1.</span> <span class="toc-text">常见算术运算</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%8F%8D%E8%BF%90%E7%AE%97"><span class="toc-number">1.11.3.2.</span> <span class="toc-text">反运算</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%80%E5%85%83%E6%93%8D%E4%BD%9C%E7%AC%A6"><span class="toc-number">1.11.3.3.</span> <span class="toc-text">一元操作符</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E5%88%B6"><span class="toc-number">1.11.4.</span> <span class="toc-text">定制</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%B1%9E%E6%80%A7%E8%AE%BF%E9%97%AE"><span class="toc-number">1.11.5.</span> <span class="toc-text">属性访问</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%8F%8F%E8%BF%B0%E7%AC%A6%EF%BC%88property%E5%8E%9F%E7%90%86%EF%BC%89"><span class="toc-number">1.11.6.</span> <span class="toc-text">描述符（property原理）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E5%88%B6%E5%BA%8F%E5%88%97"><span class="toc-number">1.11.7.</span> <span class="toc-text">定制序列</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%BF%AD%E4%BB%A3%E5%99%A8"><span class="toc-number">1.11.8.</span> <span class="toc-text">迭代器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E5%99%A8"><span class="toc-number">1.11.9.</span> <span class="toc-text">生成器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E7%94%9F%E6%88%90%E5%99%A8%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">1.11.10.</span> <span class="toc-text">生成器表达式</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%A8%A1%E5%9D%97-2"><span class="toc-number">1.12.</span> <span class="toc-text">模块</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AF%BC%E5%85%A5%E6%A8%A1%E5%9D%97"><span class="toc-number">1.12.1.</span> <span class="toc-text">导入模块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#name-%E5%B1%9E%E6%80%A7"><span class="toc-number">1.12.2.</span> <span class="toc-text">__name__属性</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%88%AC%E8%99%AB"><span class="toc-number">1.13.</span> <span class="toc-text">爬虫</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#urllib%E6%A8%A1%E5%9D%97"><span class="toc-number">1.13.1.</span> <span class="toc-text">urllib模块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#request%E5%BA%93"><span class="toc-number">1.13.2.</span> <span class="toc-text">request库</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Xpath%E6%A8%A1%E5%9D%97"><span class="toc-number">1.13.3.</span> <span class="toc-text">Xpath模块</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%A4%BA%E4%BE%8B"><span class="toc-number">1.13.3.1.</span> <span class="toc-text">示例</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Python%E5%B9%B6%E8%A1%8C%E5%8C%96"><span class="toc-number">1.13.4.</span> <span class="toc-text">Python并行化</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="toc-number">1.14.</span> <span class="toc-text">正则表达式</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#re%E6%A8%A1%E5%9D%97"><span class="toc-number">1.14.1.</span> <span class="toc-text">re模块</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%AD%A3%E5%88%99%E7%9F%A5%E8%AF%86"><span class="toc-number">1.14.2.</span> <span class="toc-text">正则知识</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E7%B1%BB"><span class="toc-number">1.14.2.1.</span> <span class="toc-text">字符类</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%87%8D%E5%A4%8D%E5%8C%B9%E9%85%8D"><span class="toc-number">1.14.2.2.</span> <span class="toc-text">重复匹配</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%85%83%E5%AD%97%E7%AC%A6"><span class="toc-number">1.14.2.3.</span> <span class="toc-text">元字符</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E4%BE%8B"><span class="toc-number">1.14.3.</span> <span class="toc-text">实例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%B4%AA%E5%A9%AA-%E5%92%8C-%E9%9D%9E%E8%B4%AA%E5%A9%AA"><span class="toc-number">1.14.4.</span> <span class="toc-text">贪婪 和 非贪婪</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9E%E8%B7%B5%E9%81%87%E5%88%B0%E7%9A%84%E9%97%AE%E9%A2%98"><span class="toc-number">1.14.5.</span> <span class="toc-text">实践遇到的问题</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Scrapy-%E7%88%AC%E8%99%AB%E6%A1%86%E6%9E%B6"><span class="toc-number">1.15.</span> <span class="toc-text">Scrapy 爬虫框架</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%A1%86%E6%9E%B6%E6%B5%81%E7%A8%8B"><span class="toc-number">1.15.1.</span> <span class="toc-text">框架流程</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E3%80%901%E3%80%91-%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AAscrpy%E6%A1%86%E6%9E%B6%E9%A1%B9%E7%9B%AE"><span class="toc-number">1.15.1.1.</span> <span class="toc-text">**【1】**创建一个scrpy框架项目</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#settings-py"><span class="toc-number">1.15.1.1.1.</span> <span class="toc-text">settings.py</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#pipelines-py"><span class="toc-number">1.15.1.1.2.</span> <span class="toc-text">pipelines.py</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#middlewares-py"><span class="toc-number">1.15.1.1.3.</span> <span class="toc-text">middlewares.py</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E3%80%902%E3%80%91-%E5%AE%9A%E4%B9%89Items-py"><span class="toc-number">1.15.1.2.</span> <span class="toc-text">**【2】**定义Items.py</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E3%80%903%E3%80%91-%E5%9C%A8-dirname-spiders-%E5%88%9B%E5%BB%BApython%E6%96%87%E4%BB%B6"><span class="toc-number">1.15.1.3.</span> <span class="toc-text">**【3】**在[dirname]&#x2F;spiders&#x2F;创建python文件</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Scrapy-shell"><span class="toc-number">1.15.2.</span> <span class="toc-text">Scrapy shell</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#Selector%E9%80%89%E6%8B%A9%E5%99%A8"><span class="toc-number">1.15.3.</span> <span class="toc-text">Selector选择器</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%AF%B7%E6%B1%82%E4%BC%A0%E5%8F%82"><span class="toc-number">1.15.4.</span> <span class="toc-text">请求传参</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%9B%BE%E7%89%87%E7%88%AC%E5%8F%96"><span class="toc-number">1.15.5.</span> <span class="toc-text">图片爬取</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#selenium"><span class="toc-number">1.16.</span> <span class="toc-text">selenium</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%89%E8%A3%85-2"><span class="toc-number">1.16.1.</span> <span class="toc-text">安装</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8"><span class="toc-number">1.16.2.</span> <span class="toc-text">使用</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%AE%9A%E4%BD%8D%E5%85%83%E7%B4%A0"><span class="toc-number">1.16.3.</span> <span class="toc-text">定位元素</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%9A%90%E5%BC%8F%E7%AD%89%E5%BE%85"><span class="toc-number">1.16.3.1.</span> <span class="toc-text">隐式等待</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%A1%8C%E4%B8%BA%E9%93%BE"><span class="toc-number">1.16.4.</span> <span class="toc-text">行为链</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#frame%E5%88%87%E6%8D%A2-%E7%AA%97%E5%8F%A3%E5%88%87%E6%8D%A2"><span class="toc-number">1.16.5.</span> <span class="toc-text">frame切换&#x2F;窗口切换</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E6%A1%86"><span class="toc-number">1.16.6.</span> <span class="toc-text">选择框</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#radio-%E5%8D%95%E9%80%89%E6%A1%86"><span class="toc-number">1.16.6.1.</span> <span class="toc-text">radio 单选框</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#checkbox-%E5%A4%8D%E9%80%89%E6%A1%86"><span class="toc-number">1.16.6.2.</span> <span class="toc-text">checkbox 复选框</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#select"><span class="toc-number">1.16.6.3.</span> <span class="toc-text">select</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#select-%E5%8D%95%E9%80%89%E4%B8%8B%E6%8B%89%E6%A1%86"><span class="toc-number">1.16.6.3.1.</span> <span class="toc-text">select 单选下拉框</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#select-%E5%A4%9A%E9%80%89%E6%A1%86"><span class="toc-number">1.16.6.3.2.</span> <span class="toc-text">select 多选框</span></a></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%9B%BE%E5%BD%A2%E7%95%8C%E9%9D%A2"><span class="toc-number">1.17.</span> <span class="toc-text">图形界面</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#pyside2"><span class="toc-number">1.17.1.</span> <span class="toc-text">pyside2</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%AE%89%E8%A3%85-3"><span class="toc-number">1.17.1.1.</span> <span class="toc-text">安装</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8E%A7%E4%BB%B6"><span class="toc-number">1.17.1.2.</span> <span class="toc-text">控件</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%8C%89%E9%92%AE"><span class="toc-number">1.17.1.2.1.</span> <span class="toc-text">按钮</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%EF%BC%9A%E8%A2%AB%E7%82%B9%E5%87%BB"><span class="toc-number">1.17.1.2.1.1.</span> <span class="toc-text">信号：被点击</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%94%B9%E5%8F%98%E6%96%87%E6%9C%AC"><span class="toc-number">1.17.1.2.1.2.</span> <span class="toc-text">方法：改变文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E7%A6%81%E7%94%A8%E3%80%81%E5%90%AF%E7%94%A8"><span class="toc-number">1.17.1.2.1.3.</span> <span class="toc-text">方法：禁用、启用</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%8D%95%E8%A1%8C%E6%96%87%E6%9C%AC%E6%A1%86"><span class="toc-number">1.17.1.2.2.</span> <span class="toc-text">单行文本框</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%EF%BC%9A%E6%96%87%E6%9C%AC%E8%A2%AB%E4%BF%AE%E6%94%B9"><span class="toc-number">1.17.1.2.2.1.</span> <span class="toc-text">信号：文本被修改</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%EF%BC%9A%E6%8C%89%E4%B8%8B%E5%9B%9E%E8%BD%A6%E9%94%AE"><span class="toc-number">1.17.1.2.2.2.</span> <span class="toc-text">信号：按下回车键</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%8E%B7%E5%8F%96%E6%96%87%E6%9C%AC"><span class="toc-number">1.17.1.2.2.3.</span> <span class="toc-text">方法：获取文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%AE%BE%E7%BD%AE%E6%8F%90%E7%A4%BA"><span class="toc-number">1.17.1.2.2.4.</span> <span class="toc-text">方法：设置提示</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%AE%BE%E7%BD%AE%E6%96%87%E6%9C%AC"><span class="toc-number">1.17.1.2.2.5.</span> <span class="toc-text">方法：设置文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%B8%85%E9%99%A4%E6%89%80%E6%9C%89%E6%96%87%E6%9C%AC"><span class="toc-number">1.17.1.2.2.6.</span> <span class="toc-text">方法：清除所有文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%8B%B7%E8%B4%9D%E6%96%87%E6%9C%AC%E5%88%B0%E5%89%AA%E8%B4%B4%E6%9D%BF"><span class="toc-number">1.17.1.2.2.7.</span> <span class="toc-text">方法：拷贝文本到剪贴板</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E7%B2%98%E8%B4%B4%E5%89%AA%E8%B4%B4%E6%9D%BF%E6%96%87%E6%9C%AC"><span class="toc-number">1.17.1.2.2.8.</span> <span class="toc-text">方法：粘贴剪贴板文本</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%A4%9A%E8%A1%8C%E7%BA%AF%E6%96%87%E6%9C%AC%E6%A1%86"><span class="toc-number">1.17.1.2.3.</span> <span class="toc-text">多行纯文本框</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%EF%BC%9A%E6%96%87%E6%9C%AC%E8%A2%AB%E4%BF%AE%E6%94%B9-2"><span class="toc-number">1.17.1.2.3.1.</span> <span class="toc-text">信号：文本被修改</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%EF%BC%9A%E5%85%89%E6%A0%87%E4%BD%8D%E7%BD%AE%E6%94%B9%E5%8F%98"><span class="toc-number">1.17.1.2.3.2.</span> <span class="toc-text">信号：光标位置改变</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%8E%B7%E5%8F%96%E6%96%87%E6%9C%AC-2"><span class="toc-number">1.17.1.2.3.3.</span> <span class="toc-text">方法：获取文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E9%80%89%E4%B8%AD%E6%96%87%E6%9C%AC"><span class="toc-number">1.17.1.2.3.4.</span> <span class="toc-text">获取选中文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%AE%BE%E7%BD%AE%E6%8F%90%E7%A4%BA-2"><span class="toc-number">1.17.1.2.3.5.</span> <span class="toc-text">方法：设置提示</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%AE%BE%E7%BD%AE%E6%96%87%E6%9C%AC-2"><span class="toc-number">1.17.1.2.3.6.</span> <span class="toc-text">方法：设置文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E5%9C%A8%E6%9C%AB%E5%B0%BE%E6%B7%BB%E5%8A%A0%E6%96%87%E6%9C%AC"><span class="toc-number">1.17.1.2.3.7.</span> <span class="toc-text">方法：在末尾添加文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E5%9C%A8%E5%85%89%E6%A0%87%E5%A4%84%E6%8F%92%E5%85%A5%E6%96%87%E6%9C%AC"><span class="toc-number">1.17.1.2.3.8.</span> <span class="toc-text">方法：在光标处插入文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%B8%85%E9%99%A4%E6%89%80%E6%9C%89%E6%96%87%E6%9C%AC-2"><span class="toc-number">1.17.1.2.3.9.</span> <span class="toc-text">方法：清除所有文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%8B%B7%E8%B4%9D%E6%96%87%E6%9C%AC%E5%88%B0%E5%89%AA%E8%B4%B4%E6%9D%BF-2"><span class="toc-number">1.17.1.2.3.10.</span> <span class="toc-text">方法：拷贝文本到剪贴板</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E7%B2%98%E8%B4%B4%E5%89%AA%E8%B4%B4%E6%9D%BF%E6%96%87%E6%9C%AC-2"><span class="toc-number">1.17.1.2.3.11.</span> <span class="toc-text">方法：粘贴剪贴板文本</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%96%87%E6%9C%AC%E6%B5%8F%E8%A7%88%E6%A1%86"><span class="toc-number">1.17.1.2.4.</span> <span class="toc-text">文本浏览框</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E5%9C%A8%E6%9C%AB%E5%B0%BE%E6%B7%BB%E5%8A%A0%E6%96%87%E6%9C%AC-2"><span class="toc-number">1.17.1.2.4.1.</span> <span class="toc-text">方法：在末尾添加文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E5%9C%A8%E5%85%89%E6%A0%87%E5%A4%84%E6%8F%92%E5%85%A5%E6%96%87%E6%9C%AC-2"><span class="toc-number">1.17.1.2.4.2.</span> <span class="toc-text">方法：在光标处插入文本</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%96%87%E6%9C%AC%E6%A0%87%E7%AD%BE"><span class="toc-number">1.17.1.2.5.</span> <span class="toc-text">文本标签</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%94%B9%E5%8F%98%E6%96%87%E6%9C%AC-2"><span class="toc-number">1.17.1.2.5.1.</span> <span class="toc-text">方法：改变文本</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%98%BE%E7%A4%BA%E5%9B%BE%E7%89%87"><span class="toc-number">1.17.1.2.5.2.</span> <span class="toc-text">显示图片</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%BB%84%E5%90%88%E9%80%89%E6%8B%A9%E6%A1%86"><span class="toc-number">1.17.1.2.6.</span> <span class="toc-text">组合选择框</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%EF%BC%9A%E9%80%89%E9%A1%B9%E6%94%B9%E5%8F%98"><span class="toc-number">1.17.1.2.6.1.</span> <span class="toc-text">信号：选项改变</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%B7%BB%E5%8A%A0%E4%B8%80%E4%B8%AA%E9%80%89%E9%A1%B9"><span class="toc-number">1.17.1.2.6.2.</span> <span class="toc-text">方法：添加一个选项</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%B7%BB%E5%8A%A0%E5%A4%9A%E4%B8%AA%E9%80%89%E9%A1%B9"><span class="toc-number">1.17.1.2.6.3.</span> <span class="toc-text">方法：添加多个选项</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%B8%85%E7%A9%BA%E9%80%89%E9%A1%B9"><span class="toc-number">1.17.1.2.6.4.</span> <span class="toc-text">方法：清空选项</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E9%80%89%E9%A1%B9%E6%96%87%E6%9C%AC"><span class="toc-number">1.17.1.2.6.5.</span> <span class="toc-text">方法：获取当前选项文本</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%88%97%E8%A1%A8-2"><span class="toc-number">1.17.1.2.7.</span> <span class="toc-text">列表</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%B7%BB%E5%8A%A0%E4%B8%80%E4%B8%AA%E9%80%89%E9%A1%B9-2"><span class="toc-number">1.17.1.2.7.1.</span> <span class="toc-text">方法：添加一个选项</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%B7%BB%E5%8A%A0%E5%A4%9A%E4%B8%AA%E9%80%89%E9%A1%B9-2"><span class="toc-number">1.17.1.2.7.2.</span> <span class="toc-text">方法：添加多个选项</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E5%88%A0%E9%99%A4%E4%B8%80%E4%B8%AA%E9%80%89%E9%A1%B9"><span class="toc-number">1.17.1.2.7.3.</span> <span class="toc-text">方法：删除一个选项</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%B8%85%E7%A9%BA%E9%80%89%E9%A1%B9-2"><span class="toc-number">1.17.1.2.7.4.</span> <span class="toc-text">方法：清空选项</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E9%80%89%E9%A1%B9%E6%96%87%E6%9C%AC-2"><span class="toc-number">1.17.1.2.7.5.</span> <span class="toc-text">方法：获取当前选项文本</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%A1%A8%E6%A0%BC"><span class="toc-number">1.17.1.2.8.</span> <span class="toc-text">表格</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E5%88%9B%E5%BB%BA%E5%88%97-%E5%92%8C-%E6%A0%87%E9%A2%98%E6%A0%8F"><span class="toc-number">1.17.1.2.8.1.</span> <span class="toc-text">创建列 和 标题栏</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%AE%BE%E7%BD%AE%E8%A1%A8%E6%A0%BC%E8%A1%8C%E6%95%B0"><span class="toc-number">1.17.1.2.8.2.</span> <span class="toc-text">方法：设置表格行数</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%8E%B7%E5%8F%96%E6%89%80%E6%9C%89%E8%A1%8C%E6%95%B0"><span class="toc-number">1.17.1.2.8.3.</span> <span class="toc-text">方法：获取所有行数</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%BD%93%E5%89%8D%E9%80%89%E4%B8%AD%E6%98%AF%E7%AC%AC%E5%87%A0%E8%A1%8C"><span class="toc-number">1.17.1.2.8.4.</span> <span class="toc-text">方法：获取当前选中是第几行</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%8F%92%E5%85%A5%E4%B8%80%E8%A1%8C%E3%80%81%E5%88%A0%E9%99%A4%E4%B8%80%E8%A1%8C"><span class="toc-number">1.17.1.2.8.5.</span> <span class="toc-text">方法：插入一行、删除一行</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E6%B8%85%E9%99%A4-%E5%88%A0%E9%99%A4%E6%89%80%E6%9C%89%E5%86%85%E5%AE%B9"><span class="toc-number">1.17.1.2.8.6.</span> <span class="toc-text">方法：清除&#x2F;删除所有内容</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%8D%95%E5%85%83%E6%A0%BC%E6%96%87%E6%9C%AC%E7%9A%84%E5%86%85%E5%AE%B9"><span class="toc-number">1.17.1.2.8.7.</span> <span class="toc-text">方法：获取单元格文本的内容</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%AE%BE%E7%BD%AE%E5%8D%95%E5%85%83%E6%A0%BC%E6%96%87%E6%9C%AC%E5%86%85%E5%AE%B9"><span class="toc-number">1.17.1.2.8.8.</span> <span class="toc-text">方法：设置单元格文本内容</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%AE%BE%E5%AE%9A%E5%88%97%E5%AE%BD%E3%80%81%E5%AE%BD%E5%BA%A6%E8%87%AA%E5%8A%A8%E7%BC%A9%E6%94%BE"><span class="toc-number">1.17.1.2.8.9.</span> <span class="toc-text">方法：设定列宽、宽度自动缩放</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%EF%BC%9A%E5%8D%95%E5%85%83%E6%A0%BC%E5%86%85%E5%AE%B9%E6%94%B9%E5%8A%A8"><span class="toc-number">1.17.1.2.8.10.</span> <span class="toc-text">信号：单元格内容改动</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%8D%95%E9%80%89%E6%8C%89%E9%92%AE-%E5%92%8C-%E6%8C%89%E9%92%AE%E7%BB%84"><span class="toc-number">1.17.1.2.9.</span> <span class="toc-text">单选按钮 和 按钮组</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E8%AF%B4%E6%98%8E"><span class="toc-number">1.17.1.2.9.1.</span> <span class="toc-text">说明</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%EF%BC%9A%E9%80%89%E4%B8%AD%E7%8A%B6%E6%80%81%E6%94%B9%E5%8F%98"><span class="toc-number">1.17.1.2.9.2.</span> <span class="toc-text">信号：选中状态改变</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%8B%BE%E9%80%89%E6%8C%89%E9%92%AE-%E5%92%8C-%E6%8C%89%E9%92%AE%E7%BB%84"><span class="toc-number">1.17.1.2.10.</span> <span class="toc-text">勾选按钮 和 按钮组</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E8%AF%B4%E6%98%8E-2"><span class="toc-number">1.17.1.2.10.1.</span> <span class="toc-text">说明</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E4%BF%A1%E5%8F%B7%EF%BC%9A%E9%80%89%E4%B8%AD%E7%8A%B6%E6%80%81%E6%94%B9%E5%8F%98-2"><span class="toc-number">1.17.1.2.10.2.</span> <span class="toc-text">信号：选中状态改变</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#tab%E9%A1%B5%E6%8E%A7%E4%BB%B6"><span class="toc-number">1.17.1.2.11.</span> <span class="toc-text">tab页控件</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#tab%E9%A1%B5%E4%B8%AD%E5%B8%83%E5%B1%80Layout"><span class="toc-number">1.17.1.2.11.1.</span> <span class="toc-text">tab页中布局Layout</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%BF%9B%E5%BA%A6%E6%9D%A1"><span class="toc-number">1.17.1.2.12.</span> <span class="toc-text">进度条</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E8%AF%B4%E6%98%8E-3"><span class="toc-number">1.17.1.2.12.1.</span> <span class="toc-text">说明</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%95%B0%E5%AD%97%E8%BE%93%E5%85%A5%E6%A1%86"><span class="toc-number">1.17.1.2.13.</span> <span class="toc-text">数字输入框</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%95%B0%E5%AD%97"><span class="toc-number">1.17.1.2.13.1.</span> <span class="toc-text">获取数字</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E6%96%B9%E6%B3%95%EF%BC%9A%E8%AE%BE%E7%BD%AE%E6%95%B0%E5%AD%97"><span class="toc-number">1.17.1.2.13.2.</span> <span class="toc-text">方法：设置数字</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%97%A5%E6%9C%9F%E6%8E%A7%E4%BB%B6"><span class="toc-number">1.17.1.2.14.</span> <span class="toc-text">日期控件</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%97%A5%E6%9C%9F"><span class="toc-number">1.17.1.2.14.1.</span> <span class="toc-text">获取日期</span></a></li></ol></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E6%96%87%E4%BB%B6%E6%A1%86"><span class="toc-number">1.17.1.2.15.</span> <span class="toc-text">选择文件框</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E7%9B%AE%E5%BD%95"><span class="toc-number">1.17.1.2.15.1.</span> <span class="toc-text">选择目录</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E5%8D%95%E4%B8%AA%E6%96%87%E4%BB%B6"><span class="toc-number">1.17.1.2.15.2.</span> <span class="toc-text">选择单个文件</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#%E9%80%89%E6%8B%A9%E5%A4%9A%E4%B8%AA%E6%96%87%E4%BB%B6"><span class="toc-number">1.17.1.2.15.3.</span> <span class="toc-text">选择多个文件</span></a></li></ol></li></ol></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BA%93%E6%96%B9%E6%B3%95"><span class="toc-number">1.18.</span> <span class="toc-text">库方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#random-%E5%BA%93"><span class="toc-number">1.19.</span> <span class="toc-text">random 库</span></a></li></ol></li></ol></div></div></div></div></main><footer id="footer" style="background-image: url('http://forgo7ten.gitee.io/store/imgs/12.jpg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By Forgo7ten</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">Hi, welcome to my <a href="https://Forgo7ten.github.io/">blog</a>!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><a id="to_comment" href="#post-comment" title="直达评论"><i class="fas fa-comments"></i></a><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">本地搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div></div></div><div id="search-mask"></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script src="https://cdn.jsdelivr.net/npm/vanilla-lazyload/dist/lazyload.iife.min.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script src="/js/search/local-search.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"><script>function addGitalkSource () {
  const ele = document.createElement('link')
  ele.rel = 'stylesheet'
  ele.href= 'https://cdn.jsdelivr.net/npm/gitalk/dist/gitalk.min.css'
  document.getElementsByTagName('head')[0].appendChild(ele)
}

function loadGitalk () {
  function initGitalk () {
    var gitalk = new Gitalk(Object.assign({
      clientID: '9e01c692fcf5a9e47867',
      clientSecret: '6d5297eda0643a171a9e5510f5ea1c29ad851389',
      repo: 'Forgo7ten.github.io',
      owner: 'Forgo7ten',
      admin: ['Forgo7ten'],
      id: 'a7e99f6496b73a880b16c65fd4fe10b5',
      updateCountCallback: commentCount
    },{"language":"en","perPage":20,"distractionFreeMode":true,"pagerDirection":"last","createIssueManually":true}))

    gitalk.render('gitalk-container')
  }

  if (typeof Gitalk === 'function') initGitalk()
  else {
    addGitalkSource()
    getScript('https://cdn.jsdelivr.net/npm/gitalk@latest/dist/gitalk.min.js').then(initGitalk)
  }
}

function commentCount(n){
  let isCommentCount = document.querySelector('#post-meta .gitalk-comment-count')
  if (isCommentCount) {
    isCommentCount.innerHTML= n
  }
}

if ('Gitalk' === 'Gitalk' || !false) {
  if (false) btf.loadComment(document.getElementById('gitalk-container'), loadGitalk)
  else loadGitalk()
} else {
  function loadOtherComment () {
    loadGitalk()
  }
}</script></div><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = true;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="false" data-text="ヾ(≧▽≦*)o 点个收藏叭,(*^▽^*),w(ﾟДﾟ)w,ㄟ(≧◇≦)ㄏ,(/≧▽≦)/点个关注叭,Σ(っ °Д °;)っ,(●ˇ∀ˇ●),( •̀ ω •́ )✧,(*/ω＼*),ヽ(*。&gt;Д&lt;)o゜真的不收藏一下嘛,(～o￣3￣)～,ヽ（≧□≦）ノ" data-fontsize="15px" data-random="false" async="async"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"scale":1,"hHeadPos":0.5,"vHeadPos":0.618,"jsonPath":"/live2dw/assets/z16.model.json"},"display":{"superSample":2,"width":200,"height":400,"position":"right","hOffset":0,"vOffset":-20},"mobile":{"show":true,"scale":0.5},"react":{"opacityDefault":0.7,"opacityOnHover":0.2},"log":false});</script></body></html>